views:

755

answers:

2

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 → Foo

Entity 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?

A: 

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:

Frederik Gheysels
+3  A: 
Richard Szalay
Hi Richard - thanks heaps for this post. There's a lot of data here so i'll try and work through this as quickly as possible before the time runs out. In your first step, are you saying that i create TWO poco's - one called `Foo` and the other `FooView` ? Also, when your post talks about `entity types`, is this what i refer to as my POCO class? and `multiple entity sets`, this means my *tables* that are on my designer (eg. table Foo and view FooView) ?
Pure.Krome
In my examples I wasn't using POCOs, but the concept is the same. The main thing is to keep two separate entity sets, and two different **storage** EntityTypes but one only **conceptual** EntityType.
Richard Szalay
Also conceptual EntityTypes are the same as your POCOs. Storage EntityTypes are the representation of the database table.
Richard Szalay
Richad, I can't get past step two because of a bug in EF4 Beta2 (rumour has it that it's fixed in RTM: http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/5c70420d-ec7b-400c-a05b-f1bb17ab4590) That said, if i change the xml file, will this then get reset if i refresh/update the designer from the DB?
Pure.Krome
Just continue past step 2 anyway (you'll see that key problem is covered in step 5). No, I think you have to explicitly request it to update the view.
Richard Szalay
Ok - so far so good. I followed all the steps and it compiles. Now, i'm not sure how to generate an object that now references `FooView`. I mean, my `Foo` EntityModel now maps to two things -> `(table) Foo` and `(View) FooView`. So how does this last step work? At first i thought it was `IObjectSet<Foo> fooViews = CreateObjectSet<Foo>("FooView")` but that doesn't work because in the 3rd step, we removed the Entity `FooView` from the EF Model. And the Entity `Foo` now has TWO (database) mappings .. so how does it know which one to use/reference?
Pure.Krome
There should be two (storage) entity sets defined in your entity model (open it as XML), so you should be able to use `CreateObjectSet<Foo>("FooView")`
Richard Szalay
Yeah, I thought so too. I do have two entity models -> http://i46.tinypic.com/2hrh92a.png Foo = LogEntries, FooView = ConnectedPlayersView ... but I still get the exception: `System.InvalidOperationException: The EntitySet name 'Entities.FooView' could not be found.`
Pure.Krome
You should also have two Entity *Sets* defined. It's possible you'll need to add it manually.
Richard Szalay
Yep, i have those two sets already in here.... http://i45.tinypic.com/30m5pv7.png Could it be a Namespace issue, in the string argument which I ass into the static method `CreateObjectSet<LogEntry>(..)` ?
Pure.Krome
Ok, I now have a working (including runtime) example. See my update above.
Richard Szalay
Ahh. i was missing `FooView` from the conceptual model. Besides hardcoding that into the conceptual model, how does it normally get there? when i drop thew view onto the designer? because if so, i thought i had to delete `FooView` from the designer AFTER i did the mapping... ??? (step 3). When that happens I think it is removed from the conceptual model.... ???
Pure.Krome
Also, this is a screenie of my Conceptual model. i just don't understand your Update Step #2.... http://i49.tinypic.com/2mm8tu9.png
Pure.Krome
That is a screenshot of your mapping (`edmx:Mapping`) not your conceptual model (`edmx:ConceptualModels`). To fix your mapping: Remove the `IsTypeOf` `EntityTypeMapping`, duplicate the `EntitySetMapping`, change `EntitySetMapping/@Name` to "ConnectedPlayersView", leave `EntitySetMapping/EntityTypeMapping/@TypeName` alone, change `EntitySetMapping/EntityTypeMapping/MappingFragment/@StoreEntitySet` to "ConnectedPlayersView"
Richard Szalay
Awesome :) works! Bummer that the designer doesn't work any more :( Not sure why that is.... Is that the same for you, Richard? (`ala: Warning ; Error 11003: Entity type 'LogEntry (Foo)' exists in multiple entity sets: LogEntries (Foos) , ConnectedPlayersView (FoosView).`)
Pure.Krome
I get the same warning. Hopefully that limitation is fixed in the RTM
Richard Szalay
Cheers Richard for working with me to solve this issue. I sincerly appreciate it. Thanks kindly!
Pure.Krome