删除对象及其相关实体 [英] Delete Object and its related entities

查看:92
本文介绍了删除对象及其相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何删除一个对象,它的所有相关实体。



例如我有表,产品,类别,产品分类和产品详细的产品分类是加入这两个产品和类别表。



我从的 http://msdn.microsoft.com/en-us/library/bb738580.aspx




删除父对象还删除在
制约关系中的所有子对象。这样的结果是一样的使
关于协会的关系 CascadeDelete 属性。




我使用这个代码:

 产品productObj = this.ObjectContext.Product.Where(p = > p.ProductID.Equals(的productID))第一()。 

如果(productObj.ProductCategory.IsLoaded!)
productObj.ProductCategory.Load();

如果(productObj.ProductDetails.IsLoaded!)
productObj.ProductDetails.Load();

//我自己的方法。
base.Delete(productObj);
base.SaveAllObjectChanges();



但我在 ObjectContext.SaveChanges()得到一个错误;




一个关系被添加或从删除 AssociationSet FK_ProductCategory_Product。随着基数的限制,相应的产品分类还必须添加或删除。



解决方案

谢谢您的答复。我已经解决了我的问题。使用在EDMX同一级联。
对于解释我正在回答我的问题。 :)



我们必须添加< OnDelete行动=瀑布>< / OnDelete> 中在两个部分。




  1. 在SSDL部分

  2. 在CSDL部分EDMX文件


Does anyone know how to delete an object and all of it's related entities.

For example I have tables, Products, Category, ProductCategory and productDetails, the productCategory is joining table of both Product and Category.

I have read from http://msdn.microsoft.com/en-us/library/bb738580.aspx that

Deleting the parent object also deletes all the child objects in the constrained relationship. This result is the same as enabling the CascadeDelete property on the association for the relationship.

I am using this code:

Product productObj = this.ObjectContext.Product.Where(p => p.ProductID.Equals(productID)).First();

if (!productObj.ProductCategory.IsLoaded)
    productObj.ProductCategory.Load();

if (!productObj.ProductDetails.IsLoaded)
    productObj.ProductDetails.Load();

//my own methods.
base.Delete(productObj);
base.SaveAllObjectChanges();

But I am getting an error on ObjectContext.SaveChanges(); I.e.,

A relationship is being added or deleted from an AssociationSet 'FK_ProductCategory_Product'. With cardinality constraints, a corresponding 'ProductCategory' must also be added or deleted.

解决方案

Thanks for the reply. I have solved my problem. Using the same cascade in EDMX. For the explanation i am answering my own question. :)

We have to add <OnDelete Action="Cascade"></OnDelete> in EDMX file at two portions

  1. In SSDL portion
  2. In CSDL portion

这篇关于删除对象及其相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆