In this article, i'll will show how to manage a many-to-many relationship in Entity Framework 4.
First of all, lets's see the representation of the basic model, from power designer :
Here we have two entities named ENTITY_1 and ENTITY_2. One instance of ENTITY_1 may have 1 or more instance of ENTITY_2 and vice-versa. One instance of ENTITY_1 (ENTITY_2) is identified uniquely by a property called ENTITY1_ID (ENTITY2_ID).
At the PMD, we have a physical representation like this
And the corresponding SQL Server Diagram :
So, the question is how to link entries in ENTITY_1 with entries in ENTITY_2 by filling the association table RELATION_1. In TSQL, we need to insert explicitly this combination by doing something like :
INSERT INTO [RELATION_1] VALUES (.......)
This concept doesn't exist explicitly in EF as seen in the generated diagram :
The RELATION_1 table, which exist for real in the sys.tables of the current database, is not mapped as a real entity at the EF level.
After running this code, take a look at all involved tables :
As we can see, the table RELATION_1 is auto filled with the corresponding association.
Consider the case where the association entity has an attribute named value :
The generated edmx will show a concrete entity named relation_1 having an attribute named value:
As we can see, things become more explicit because EF has created a new entity mapping the RELATION_1 table.
Inserting becomes more easier as show :
No comments:
Post a Comment