简体   繁体   中英

Delete entity with non-nullable foreign key relationship entity framework 5 POCO

I have a one to many relationship with a non nullable foreign key column on the child entity. I'm trying to delete a record from the child table but can't seem to get it to work.

I end up with a message similar to the following - A referential integrity constraint violation occurred: A primary key property that is a part of referential integrity constraint cannot be changed when the dependent object is Unchanged unless it is being set to the association's principal object. The principal object must be tracked and not marked for deletion

I am attempting to delete the object just by calling context.Set().Remove(entity)...

I have tried removing the entity from the parent collection and then deleting it before calling save changes. I have also tried just deleting the child entity directly but no matter what I do, I can't seem to get it to work.

What is the proper way of deleting a child entity that has a non-nullable foreign key relationship in entity framework 5?

Here is my mapping configuration :

实体映射

This works --

var item = this.unitOfWork.PersonalListItemRepository.GetFirst(x => x.PersonalListID == 45146 && x.ItemCode == "1030943" && x.UOM == "EA");
        this.unitOfWork.PersonalListItemRepository.Delete(item);

The delete implementation is simply --

this.context.Set<T>().Remove(item);

However this will not work --

var listToUpdate =
this.unitOfWork.PersonalListRepository.FindByExpression(
    x => x.PersonalListID == personalList.PersonalListID).First();
var entityToDelete = listToUpdate.PersonalListItems.First();

this.unitOfWork.PersonalListItemRepository.Delete(entityToDelete);

Implementation of FindByExpression --

 public ICollection<T> FindByExpression(Expression<Func<T, bool>> expression)
    {
        return context.Set<T>().Where(expression).ToList();
    }

I have also tried to remove the item from the parent entities collection and just update the parent. I suppose I can, once I've gotten the list if children I need to delete, reload them from the context and then delete them (but this seems like it is unnecessary being that the items were loaded from my context to begin with). I must be missing something major here...

Okay, so the problem here ended up being that the items that I ended up trying to delete (or modify/attach to the context) were not the proxies that were pulled from the original query to the database. So, the entity framework was complaining because it though I was trying to modify the primary key of an entity it was already tracking with a new entities value (the exception, although correct, was misleading).

The solution here is to make certain that I am not trying to attach or update with a non-proxy entity when the context has already started tracking a proxy version of the same entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM