原因何在? 究竟发生了什么? 真正的原因是我们所处理的实体与其他实体之间存在关联。如图1所示:
Figure 1: The entity with association
如果移除Employee表中的所有关系,然后重新生成数据模型,测试就能够通过了。
如何解决这个问题呢?显然,显式地去除表的关联并非解决这一问题的非常好的方法。这样会影响整个数据模型。对此,Steve Michelotti提出了一个解决方案,也就是利用partial类,为每个数据实体提供一个Detach方法,用以移除实体间的关系:
public partial class Contact
{
public void Detach()
{
foreach (Address address in this.Addresses)
{
address.Detach();
}
}
}
public partial class Address
{
public void Detach()
{
this._AddressType = default(EntityRef<AddressType>);
this._State = default(EntityRef<State>);
}
}
{
public void Detach()
{
foreach (Address address in this.Addresses)
{
address.Detach();
}
}
}
public partial class Address
{
public void Detach()
{
this._AddressType = default(EntityRef<AddressType>);
this._State = default(EntityRef<State>);
}
}