EF - Remove a relationship from parent entity

asp.net-mvc c# entity-framework entity-framework-6

Accepted Answer

Added another edit

I also thought that you might be orphaning the other side of the relationship; you're deleting the Category entity from the Product, but are you also removing the Product from that Category? Consider using WillCascadeOnDelete (Cascade Delete for One-to-Zero-or-One Relationship in Entity Framework (EF)) or, alternatively, try include the relationships in your queries._ctx.Products.Where(...) , use_ctx.Products.Include(p => p.Categories).Where(...)

Please let us know how that goes for you.


So, judging by Lorenzo's response, I can see what he's trying to do. I've been bitten by this in the past, along with maybe a few other people.

Choose option 3 if you think you have a Cascade Delete problem (which is now not so much an option as a requirement...).

Check out Due to the non-nullability of one or more of the foreign-key attributes, the connection could not be altered..

Since we don't know the schema of CategoriesPerProduct: our perpetrator, the main problem, as best as I can tell, is that you're placing the item from CategoriesPerProduct in an impossible condition where it requires a Category to exist, but the Category has essentially been turned to null.

Consequently, you have a few choices:

  1. Nullify the foreign key (not really recommended)
  2. Directly removing the object from your DbContext (such as_ctx.Categories.Remove(category); )
  3. Declare the State to be Deleted (using language likectx.Entry(category).State = EntityState.Deleted )
5/23/2017 12:34:53 PM

Popular Answer


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow