Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping data from 2 tables to 1 entity - Entity Framework 4

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

like image 294
Christian Bennich Avatar asked Oct 07 '10 09:10

Christian Bennich


People also ask

How do you map an entity to multiple tables?

Yes, you can map an entity to 2 database tables in 2 simple steps: You need to annotate your entity with JPA's @Table and @SecondaryTable annotations and provide the names of the first and second table as the value of the name parameters.

How will you create relationship between tables in entity Framework?

You can create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.


1 Answers

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.

like image 125
Yakimych Avatar answered Sep 21 '22 05:09

Yakimych