Entity Framework v4 & # 8594; need help with POCO and entities

I am using EF4 and I have two objects that I want to map to the same POCO . I am not sure how I can do this.

Entity 1 -> Foo (this represents the table FOO in db)
POCO → Foo

Entity 2 -> FooView (this is the FooView view in 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 to do this, it compiles, but it fails with the following exception:

System.ArgumentException: System.ArgumentException: The specified object type, "MyProject.Core.Foo", does not match the type "EntityFramework.SqlServerModel.FoosView" from EntitySet 'FoosViews'.

Any suggestions?

+4
source share
2 answers

Here is a list of things to look for:

  • Your storage model should have:
    • Two EntitySets : Foo and FooView
    • Two EntityTypes : Foo and FooView
  • Your conceptual model should have:
    • Two EntitySets : Foo and FooView - both with EntityType set to ModelName.Foo
    • One EntityType : Foo
  • Your Map must have two EntitySetMappings :
    • Foo with one EntityTypeMapping ("ModelName.Foo") with one MappingFragment ("Foo")
    • FooView with one EntityTypeMapping ("ModelName. Foo ") with one MappingFragment ("FooView")

You should be able to do the following:

 Foo foo = new ModelEntities() .CreateObjectSet<Foo>("FooView") .First(); 

You can give yourself a hat by following these steps:

  • Add Foo and FooView to your model.
  • In the Mapping Details from Foo click Add a Table or View and select FooView
  • Remove FooView from your model.
  • Save the model and open it in an XML editor
  • ( pre-RTM ) Find <EntityType Name="FooView"> in <StorageModels> and remove any invalid entries from <Key> (it should match <EntityType Name="Foo"> )
  • Remove <EntityTypeMapping Name="IsTypeOf(Foo)" /> and <EntityTypeMapping Name="IsTypeOf(FooView)" /> (they caused me errors)

Starting with beta 2, the implementation above will break the constructor

+3
source

NHibernate should solve this problem with Projections. So, I think something similar should exist in the Entity Framework. I did a bit of googled work and I came across this:

+1
source

All Articles