5

I am stuck here.

Is it possible to map data from 2 different tables to 1 entity in Entity Framework 4.

I have a bunch of employees in one table, and in the other I have som project information. I would like to combine these 2 tables in one Entity, and keep the tracking features etc., is that possible?

I do not want to use a function import, but do it solely through the Entity Model.

Can anyone help - when I try to do it, i get the following error all the time:

Error 3024: Problem in mapping fragments starting at line 2354:Must specify mapping for all key properties (MyProjectTable.PSInitials, MyProjectTable.ProjectID) of the EntitySet MyProjectTable.

Both key are mapped to their respective tables. The new Entity are made with MyProjectTable as the basetable.

The relation between the 2 tables is a 1-*

Hope you can help.

/Christian

  • AFAIK you cannot do this. If they can be combined into 1 entity, why can they not be combined into one table? Or another option - just leave them as 2 entities - and `project` them into 1 entity (ie special POCO) – RPM1984 Oct 07 '10 at 09:33
  • Hmmm - found this article - http://blogs.msdn.com/b/simonince/archive/2009/03/23/mapping-two-tables-to-one-entity-in-the-entity-framework.aspx - and apparently this can be done on a 1-1 relation. But doing the same on a 1-* relation gives the following error: Error 3024: Problem in mapping fragments starting at line 2009:Must specify mapping for all key properties (MyProjectTable.PSInitials, MyProjectTable.ProjectID) of the EntitySet MyProjectTable. Any suggestions???? – Christian Bennich Oct 07 '10 at 09:47
  • If it is 1-*, then why do you want a single entity? That doesn't really make sense. – Ray Henry Oct 07 '10 at 11:55

2 Answers2

6

You cannot map two tables with a one-to-many relationship to one entity. If you don't want projecting the results into one object in code, consider creating a view and mapping it instead.

According to http://msdn.microsoft.com/en-us/library/bb896233.aspx

You should only map an entity type to multiple tables if the following conditions are true:

  • The tables to which you are mapping share a common key.

  • The entity type that is being mapped has entries in each underlying table. In other words, the entity type represents data that has a one-to-one correspondence between the two
    tables; the entity type represents an inner join of the two tables.

Yakimych
  • 17,612
  • 7
  • 52
  • 69
1

The reasons for doing this are quite straightforward - for example, a table of data points that all have one of five 'types'. Obviously the 'type' will be a separate table for the sake of normalisation, but from an application point of view (working with the data) it makes more sense to have all properties in a single entity.

So we can't do this with Entity Framework - a supposed Object-Relational-Mapper. What, then, is the point of using such a framework?

Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
  • I think that the argument would be that this is more effectively done as a view from the database rather than creating Entities that act as views. I was attempting to do this same thing myself ... I still am. Since I am a code guy and have no control over the database it would be helpful for me to be able to provide these entities this way without having to manipulate the data store ... but I understand the rationale for NOT doing it. – Gary O. Stenstrom Oct 05 '12 at 02:55