Я не могу понять, почему эта конфигурация не будет генерировать оператор вставки в БД. Я проверяю через SQL Profiler.
Вот моя модель через Visio:
Вот мой edmx (сначала это база данных). Кружок показывает отношение самоссылки обратно к GeoBoundary.
Вот мой код:
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();
}
}
}
Когда я добираюсь до parent.GeoBoundaryAssocTo.Add(child);
в отладчике, я удостоверяюсь, что родитель и дочерний элемент существуют, затем я делаю шаг вперед, но ничего не получаю в профилировщике. Что дает? Проблема в том, что оба объекта уже существуют в базе данных, и я ничего не меняю, кроме отношений? Если да, то как я могу пометить отношение как измененное, чтобы EF сгенерировал вставку?
Детали EDMX:
<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>
Dictionary<int, List<int>>
не так, я хочу, чтобы все это провалилось. - person Chris   schedule 02.01.2013