views:

65

answers:

1

I would like to know how to save new associations for entities with many to many relationships.

I've got 3 tables:

Partner -
 -- ParnterID
 -- ParnterName
 -- Etc

PartnerRegion
 -- PartnerRegionID
 -- RegionID
 -- PartnerID

Region
 -- RegionID
 -- RegionName
 -- Etc

I have the entities created, and the associations appear in my entities when I look at them in the generated linq-to-sql code. Then I query the database for a partner:

_context.Partners.Where(x => x.PartnerID == 12345);

Then I would like to clear out all the associations for PartnerRegions and then add some from information collected from the UI.

What would that code look like?

+1  A: 

Something like this should work:

var p = _context.Partners.Where(x => x.PartnerID == 12345);
p.PartnerRegions.Clear();

Loop over UI elements
  p.PartnerRegions.Add(new PartnerRegion() { property = value } );
End loop
Jimmie R. Houts