Mam problem z usuwaniem rekordów.
public class F { public virtual ICollection<FEvents> Events {get;set;} }
public class FEvents { public Guid Id {get;set; } // Key public Guid FId {get;set;} // ForeginKey }
Problem jest taki, że do repozytorium przesyłam nową kolekcję FEvents w obiekcie F i tutaj chce usunąć te, które nie występują w nowej kolekcji, dodać nowe.
Próbowałem kilka podejść:
- Działa dodawanie ale usuwanie nie - nie można dodać tego samego rekordu bo już jest.
dbEntity.Events = newEntity.Events.Select(...).ToList();
- Dodawanie nowych i usuwanie starych
// Dodawanie nowych:
dbEntity.Events.AddRange(eventsWhichAreInsideNewEntityAndNotInsidedbEntity);
// Usuwanie
for(; ; ;) // kolekcja do usunięcia
dbEntity.Events.Remove(kolekcjaDoUsunięcia[index]);
// Tutaj dodatkowo próbowałem ustawić stan na "deleted"
W punkcie drugim pojawia się problem przy SaveChanges:
"The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted."
Sęk w tym, że nie chce ustawiać foregin key na null tylko wywalić rekord z tabli.
Jak tego dokonać?