2

例如,我有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>

4

1 回答 1

2

Setting reserve to null will not delete it. You must delete it through DbSet<Reserve>'s Remove method.

于 2013-03-20T20:38:01.643 回答