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)
POCO → 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?
Here is a checklist of things to look for:
EntitySets
: Foo
and FooView
EntityTypes
: Foo
and FooView
EntitySets
: Foo
and FooView
- both with an EntityType
set to ModelName.Foo
EntityType
: Foo
EntitySetMappings
:
Foo
with one EntityTypeMapping
("ModelName.Foo") with one MappingFragment
("Foo") FooView
with one EntityTypeMapping
("ModelName. Foo ") with one MappingFragment
("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:
Foo
and FooView
to your model Mapping Details
of Foo
click Add a Table or View
and select FooView
FooView
from your model <EntityType Name="FooView">
in <StorageModels>
and remove any incorrect entries from <Key>
(it should match <EntityType Name="Foo">
) <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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.