Question

I have a model called 'target' retreived by entityframework with a collection addresses.

After removing all items that are not in excesting in another collection i'm saving my entity framework context.

However, when checking my database the records are still there. While my linq code made sure to remove the items from the collection.

Here is my linq code:

using (IUnitOfWork uow = _uow.CreateUnitOfWork())
{
    var target = _repository.GetByBron(uow, bron.BronId);
    target.Adressen.RemoveAll(x => source.Adressen.All(y => !y.Equals(x)));

    //Which calls Context.SaveChanges(); inside the unit of work class
    uow.Save(_logger);
}    

Update: The problem is not removing my record from the collection. Its when i'm calling the save on the context. The relation record in my database is still there... nothing has been deleted... aldo it was removed from the collection.

Solved I'm directly removing it from the context now. (with a seperated repository object)

Was it helpful?

Solution

This is very dependent upon the configuration of the relationships. See Entity Framework .Remove() vs. .DeleteObject()

Since your relationship sounds to be a many to many, you will likely need to call DeleteObject for the addresses themselves, as EF won't automatically delete the orphaned records.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top