I can't figure out why this configuration won't generate an insert statement to the db. I'm checking via SQL Profiler.
Here is my model via Visio:
Here is my edmx (this is database first). The circle shows the self referencing relationship back to GeoBoundary.
Here is my code:
public void UpdateAssocs(Dictionary<int, List<int>> fromTo) {
//iterate through each dictionary entry
foreach (KeyValuePair<int, List<int>> entry in fromTo) {
using (TransactionScope scope = new TransactionScope()) {
//get a reference to the parent geoboundary for this entry
GeoBoundary parent = contactContext.GeoBoundaries
.FirstOrDefault(x => x.GeoID == entry.Key);
//test to see if the parent is null, it shouldn't be b/c this dictionary was generated
// from a list of database values (but shit happens so throw an error if it is null)
if (parent != null) {
foreach (int childID in entry.Value) {
//check to see if the child exists in the parents list of children
GeoBoundary child = parent.GeoBoundaryAssocTo
.FirstOrDefault(x => x.GeoID == childID);
if (child == null) {
//get a ref to the GeoBoundary that SHOULD be tied to the parent (it should exist but there just
// isn't an established relationship in the db)
child = contactContext.GeoBoundaries
.FirstOrDefault(x => x.GeoID == childID);
//check the damn thing again b/c you never want to assume...
// but if it's still null then do nothing!
if (child != null) {
parent.GeoBoundaryAssocTo.Add(child);
contactContext.SaveChanges();
}
}
}
}
else {
throw new Exception(@"Parent GeoID passed to UpdateAssocs method or GeoID is null.");
}
scope.Complete();
}
}
}
When I get to parent.GeoBoundaryAssocTo.Add(child);
in the debugger I made sure that parent and child both exist, then I step through but I get nothing in profiler. What gives? Is it a problem that both entities already exist in the db and I'm not changing anything but the relationship? If so then how can I mark the relationship as changed so EF will generate the insert?
EDMX Details:
<AssociationSet Name="GeoBoundaryAssociation" Association="Contact.GeoBoundaryAssociation">
<End Role="GeoBoundary" EntitySet="GeoBoundaries" />
<End Role="GeoBoundary1" EntitySet="GeoBoundaries" />
</AssociationSet>
<Association Name="GeoBoundaryAssociation">
<End Type="Contact.GeoBoundary" Role="GeoBoundary" Multiplicity="*" />
<End Type="Contact.GeoBoundary" Role="GeoBoundary1" Multiplicity="*" />
</Association>