问题描述
在检查了这些SO文章之后: cascade-delete-in-entity-framework , ef6-1-soft-delete-with-cascade-delete , cascading-soft-delete ,和reasons-for-cascading-soft-deletes 并没有找到解决方案...
After checking these SO articles: cascade-delete-in-entity-framework, ef6-1-soft-delete-with-cascade-delete, cascading-soft-delete, method-for-cascading-soft-deletes-in-parent-child-relationships and reasons-for-cascading-soft-deletes and not finding a solution...
我让SoftDelete用于我的实体模型.我在上下文中覆盖了SaveChanges()
:
I have SoftDelete working for my Entity Models. I have overridden SaveChanges()
in my Context:
public override int SaveChanges()
{
ChangeTracker.DetectChanges();
foreach (DbEntityEntry<ISoftDeletable> entity in ChangeTracker.Entries<ISoftDeletable>())
{
if (entity.State == EntityState.Deleted)
{
entity.State = EntityState.Modified;
entity.Entity.IsDeleted = true;
}
}
return base.SaveChanges();
}
我为孩子实体设置了CascadeOnDelete.因为我覆盖已删除的EntityState
,所以它不会级联.有谁知道将导航属性仅放在foreach
循环中的方法吗?还是处理SoftDeletes的更好方法?
I have set CascadeOnDelete for my Child Entities. Because I override the deleted EntityState
it doesn't cascade. Does anybody know a way to put only the Navigation properties in a foreach
loop? Or a better way to handle SoftDeletes?
先谢谢您
推荐答案
阅读此SO文章后 ...
After reading this SO Article entity-framework-6-code-first-cascade-delete...
我意识到,我正在像这样抓取并删除我的实体:
I realized, I was grabbing and and deleting my entity like this:
var entity = context.Parent.FirstOrDefault();
context.Parent.Remove(entity);
当我需要像这样抓取整个图时:
When I needed to grab the entire Graph like this:
var entity = context.Parent.Include("Children").FirstOrDefault();
context.Parent.Remove(entity);
感谢您输入@Maarten
Thank you for your input @Maarten
这篇关于如何级联SoftDelete?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!