使用EF DBFirst方法进行级联删除无法正常工作。 [英] Cascade delete with EF DBFirst approach not working.

查看:157
本文介绍了使用EF DBFirst方法进行级联删除无法正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 


   我不确定它是否是EF的正确论坛。如果没有,请原谅。


我有两个表类别和产品。 


类别: - CategoryId(pk),CategoryDe​​sc


产品: - ProductId(pk),ProductDesc,CategoryId(FK,可空)。 


我的要求是当我删除某个类别时,我希望所有的该类别下的产品将被删除。  但对我来说,在产品表(categoryId列)中插入了空值。如果CategoryId不为null,则抛出异常。 


在删除hte查询之前,表格看起来像

  CategoryId CategoryDe​​sc  
C01 C1desc

ProductId ProductDesc CategoryId
p01 p1desc C01

以下是我用来删除的查询。


< pre class ="prettyprint"> var cate = ctx.Categories.Include(c => c.Products).First(f => f.categoryid ==" C01");
ctx.Categories.Remove(cate);
ctx.SaveChanges();

执行上述查询后,表格看起来像

  CategoryId CategoryDe​​sc  

ProductId ProductDesc CategoryId
p01 p1desc Null

删除p01需要做什么。当我浏览时,我被要求使用Eager加载。但那并没有解决问题。 Kidnly帮助。


谢谢,


Sanjay。


解决方案

也许,您需要在表格的数据库级别实现级联删除。\


Hi, 

   I am not sure whether its the correct forum for EF. Kindly excuse if not.

I have two tables Category and Product. 

Category :- CategoryId (pk), CategoryDesc

Product:- ProductId (pk), ProductDesc, CategoryId (FK, nullable). 

My requirement is when I delete a category, I expect all the products under that category to be deleted.  But for me null value is being inserted in the product table (categoryId column). If the CategoryId is not null then an exception is thrown. 

Before deleting hte query, table looks like

CategoryId	CategoryDesc	
C01	        C1desc	
		
ProductId	ProductDesc	CategoryId
p01	        p1desc	         C01

Following is hte query I use to delete.

            var cate = ctx.Categories.Include(c => c.Products).First(f => f.categoryid == "C01");
            ctx.Categories.Remove(cate);
            ctx.SaveChanges();

After the execution of above query, table looks like

CategoryId	CategoryDesc	
		
ProductId	ProductDesc	CategoryId
p01	        p1desc	         Null

What needs to be done to remove p01. When I browsed I was asked ot use Eager loading. but that didnt solve the issue. Kidnly help.

Thanks,

Sanjay.

解决方案

Maybe, you need to implement cascading delete at the DB level on the tables..\


这篇关于使用EF DBFirst方法进行级联删除无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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