简体   繁体   English

实体框架中的级联更新

[英]Cascade Update in Entity Framework

I have the following scenario involving 2 classes: 我有以下涉及2个课程的场景:

public class Parent
{
  [Key]
  public int Id {get;set;}

   //.. Other properties here

  public virtual IList<Child> Children {get;set;} 
}

and

public class Child
{
  [Key]
  public int Id {get;set;}
  public int ParentId {get;set;}

   //.. Other properties here

  [ForeignKey("ParentId")]
  public virtual Parent {get;set;} 
}

I also have an DbContext with the associated DbSet Children and DbSet Parents and I want to make the following update operation: 我也有相关的DbSet儿童家长DbSet一个的DbContext,我想作以下更新操作:

//.. Get some Parent instance -> convert it to ParentVM -> do some operations on ParentVM in the Service //layer () -> then try to update it back using EF: 
// parentVM now contains a modified version both in the primitive properties and also in the children  collection: some children have new values  


var parent = ConvertBackToORMModel(parentVM); //converts everything back, including the Children collection

using (var context = new ApplicationDbContext())
{
  context.Set<Parent>().AddOrUpdate(parent);
  //context.Set<Child>().AddOrUpdate(childModified); // If I do this here, it saves also the modified children back in the DB; but I want this to be **automatic when updating the parent**

  context.SaveChanges(); //here, the primitive modified properties are saved in DB, the modified children collection remains the same
} 

The problem is that, the above code snippet is generic, which means that I would need to iterate, depending on the object through the Children collection (or all virtual collections, etc.) and call context.Set().AddOrUpdate(childModified); 问题在于,上面的代码段是通用的,这意味着我需要根据通过Children集合(或所有虚拟集合等)的对象进行迭代,并调用context.Set()。AddOrUpdate(childModified) ; for each one. 每一个人。 I want this behavior to be automatic when updating the parent. 我希望这种行为在更新父项时是自动的。

Is there any way to do this? 有什么办法吗?

Thanks, Ionut 谢谢,Ionut

I believe entity framework does not have cascade update feature, 我相信实体框架没有级联更新功能,

but I know hibernate has it. 但我知道冬眠有它。

however you could do something like overwritething method savechanges() in ApplicationDbContext class similar to cascade delete mentioned here 但是,您可以在ApplicationDbContext类中执行类似于覆盖方法savechanges()的操作,类似于此处提到的层叠删除

ApplicationDbContext : DbContext
    {
               public override int SaveChanges()
                  {
                  //sets child in ram memory  entity state to modified 
                  //if its parent entity state is modified each time you call SaveChanges()
                  Child.Local.Where(r => Entry(r.Parent).State == EntityState.Modified)
                 .ToList().ForEach(r => Entry(r).State=EntityState.Modified);

                  base.SaveChanges();
                  }
   }

I think this is what you are looking for, I have not tested this 我认为这是您要寻找的,我尚未测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM