例如,我有Order
并且这个订单的Reserve
字段有ICollection<ReserveItem>
. 我现在拥有的是:
订单类
public class Order
{
...
public Reserve Reserve {get; private set;}
public void ClearReserve()
{
// Reserve.Items = null; or Resrver.Items.Clear()
Reserve = null;
}
}
预备班
public class Reserve
{
public ICollection<ReserveItems> Items {get; set;}
public DateTime ExpireDate {get; set;}
}
和OrderRepository 类
public OrderRepository
{
...
public void ClearOrderReserve(Order order)
{
order.ClearReserve();
repostiry.InsertOrUpdate(order);
}
...
}
在我的数据库上下文类中,我有:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
...
modelBuilder.Entity<Reserve>()
.HasMany(pr => pr.Items)
.WithRequired()
.WillCascadeOnDelete();
...
}
如何正确删除方法Order.Reserve
中的字段(将其值设置为null
)并在删除时启动级联以从数据库OrderRepository.ClearOrderReserve(Order order)
中删除冗余记录?ICollection<ReserveItem>