Entity Framework v4 -> need some help with POCO's and Entities
I'm using EF4 and I've got two entities that I wish to map to the same POCO
. I'm not sure how I can do this.
Entity 1 → Foo (this represents a table FOO in the db)
P开发者_如何学GoOCO → FooEntity 2 → FooView (this represents a view FooView in the db)
POCO → Foo
I understand that I need to do something like
IObjectSet<Foo> _foos = CreateObjectSet<Foo>();
// Note spelling of the Entity.
IObjectSet<Foo> _foosView = CreateObjectSet<Foo>("FooViews");
But when i try this, it does compile, but it fails with the following exception:
System.ArgumentException: System.ArgumentException: The specified entity type, 'MyProject.Core.Foo', does not match the type 'EntityFramework.SqlServerModel.FoosView' from the EntitySet 'FoosViews'.
Any suggestions?
- How to: Define a Model with Multiple Entity Sets per Type
- How to: Add an Object to a Specific Entity Set
Here is a checklist of things to look for:
- Your Storage Model should have:
- Two
EntitySets
:Foo
andFooView
- Two
EntityTypes
:Foo
andFooView
- Two
- Your Conceptual Model should have:
- Two
EntitySets
:Foo
andFooView
- both with anEntityType
set toModelName.Foo
- One
EntityType
:Foo
- Two
- Your Mapping should have two
EntitySetMappings
:Foo
with oneEntityTypeMapping
("ModelName.Foo") with oneMappingFragment
("Foo")FooView
with oneEntityTypeMapping
("ModelName.Foo") with oneMappingFragment
("FooView")
You should new be able to execute the following:
Foo foo = new ModelEntities()
.CreateObjectSet<Foo>("FooView")
.First();
You can give yourself a headstart by doing the following:
- Add
Foo
andFooView
to your model - In the
Mapping Details
ofFoo
clickAdd a Table or View
and selectFooView
- Delete
FooView
from your model - Save the model and open it in the XML editor
- (pre-RTM) Find
<EntityType Name="FooView">
in<StorageModels>
and remove any incorrect entries from<Key>
(it should match<EntityType Name="Foo">
) - Remove the
<EntityTypeMapping Name="IsTypeOf(Foo)" />
and<EntityTypeMapping Name="IsTypeOf(FooView)" />
(they caused me errors)
As of beta 2, implementing the above will break the designer
In NHibernate, one should solve this using Projections. So, I think that there must exists something similar like that in the Entity Framework. I've googled a bit, and I came accross this:
- Object Queries (Entity Framework)
精彩评论