New Posts New Posts RSS Feed: Add/Delete in many-to-many relationship
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Add/Delete in many-to-many relationship

 Post Reply Post Reply
Author
mit View Drop Down
Newbie
Newbie


Joined: 16-Feb-2010
Posts: 1
Post Options Post Options   Quote mit Quote  Post ReplyReply Direct Link To This Post Topic: Add/Delete in many-to-many relationship
    Posted: 16-Feb-2010 at 11:59pm
Hi,

I have a many-to-many relationship similar with the one you have in Many-To-Many Associations in the Entity Framework pdf, for example Employee and Territory and the link table EmployeeTerritory. As explain in the pdf I created a property in Employee class and get the associated territories. So, I need to expose the Territories list in a grid and to add and delete entities to/from that grid.

Can you tell me how can I add a new Territory entity to the Employee and how can I delete it? I tried to just create a new Territory with EntityManager.CreateEntity<Territory>() and to just add it to the Territories list, but it's not working.

Thanks,

Mit
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 17-Feb-2010 at 4:22pm
It depends upon whether your linking table has a payload or not.  In NorthwindIB, the EmployeeTerritory has a payload and therefore the Entity Framework exposes the EmployeeTerritory linking entity explicitly in the model, and models two 1-to-many associations: Employee->EmployeeTerritory and Territory->EmployeeTerritory. If I understand what you're saying, you wrote a custom property in Employee to return the collection of related Territories. So you'll need another method somewhere that, given a Territory and an Employee, will create a new EmployeeTerritory entity to link them.

If your linking entity has no payload, then EF creates the many-to-many property for you on each entity, and then the Add() and Remove() methods on the collections of related entities will cause the new linking entity to get created behind the scenes.

Note that there's also an EmployeeTerritoryNoPayload table in NorthwindIB. If you use that instead of EmployeeTerritory you can see how the EF behaves when the linking entity has no payload.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down