I am trying to delete an object from a many to many relationship but it refused to get worked.
The structure is as follows:
I have a Products table which every product has excProducts
I am trying to delete an excProduct with the following code
var objectContx = new objectContx();
var prod = objectContx.Products.Where(p => p.ProductID == prodID).FirstOrDefault();
var excProd = objectContx.ExcProducts.Where(exc => exc.ExcProductID == excProdid).FirstOrDefault();
prod.ExcProducts.Attach(excProd);
prod.ExcProducts.Remove(excProd);
objectContx.SaveChanges();
The excProd deleted from database but when I am getting the Product from objectContext it has the deleted excProd in it's list.
How could开发者_StackOverflow I delete it from the objectContext as well?
If your many to many relationship has an entity (e.g. if it has payload) then the instance of that entity must be deleted. This can be done manually or using cascaded delete (see option in property window while standing on relationship in EDMX designer).
精彩评论