【问题标题】:Remove child records from aggregate root using entity framework使用实体框架从聚合根中删除子记录
【发布时间】:2015-10-24 02:11:14
【问题描述】:

我的客户业务对象是地址集合的聚合根。

public class Customer {
  Public Customer {Addresses = new List<Address>}
  public virtual List<Address> Addresses { get; set; }
  // other properties
}

Public class Address {
  public virtual Customer customer {get;set;}
  [NotMapped]

  // other properties
}

我的上下文包含

public DBSet<Customer> Customers {get;set;}

但地址没有 DBSet,因为我希望客户成为聚合根。

但是当我使用

Customer.Addresses.RemoveAll(x=>x.TaggedToDelete)

Address 表中的 Customer_Id 设置为 null,而不是从数据库中删除 Address。

如何使用聚合根从数据库中删除地址?

我看过 How to remove child one to many related records in EF code first database?

但它使用 DBSet 作为子记录。

【问题讨论】:

    标签: entity-framework-6 aggregateroot


    【解决方案1】:
    var deleteQueue = customer.Addresses.Where(x => x.TaggedToDelete).ToArray();
     foreach (var a in deleteQueue)
     {
         var entry = connect.Entry(a);
         entry.State = EntityState.Deleted;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多