【发布时间】:2014-12-24 12:48:53
【问题描述】:
假设我有一个独立包中的类,并且可以被多个对象使用,例如:
public class TimeSlot
{
[Required]
public int Id { get; set; }
[Required]
public DateTime StartTime { get; set; }
[Required]
public DateTime EndTime { get; set; }
}
不是这个 TimeSlot 可以从以下两个类中引用:
public class Business
{
public int Id { get; set; }
public virtual IList<TimeSlot> OpeningHours { get; set; }
}
和
public class Delivery
{
public int Id { get; set; }
public virtual IList<TimeSlot> DeliveryHours { get; set; }
}
使用实体框架代码优先,这将产生一个TimeSlot 表,其中包含Business 和Delivery 对象的外键。
假设我修改了一个Business 对象(清除OpeningHours 的列表并在代码中添加新的)并更新它;更新后,旧的TimeSlot 记录会将BusinessId 外键设置为NULL,而不是被删除。
什么是正确的实现来删除这些记录,因为它们不再需要(最好不污染TimeSlot 对象)?
【问题讨论】:
标签: c# asp.net entity-framework ef-code-first code-first