【发布时间】:2012-09-21 23:32:24
【问题描述】:
我有一个试图通过 Entity Framework 4.3 访问的现有数据库。大多数表格和关系都不是问题,但是这组表格给我带来了一些我似乎无法找到答案的问题。
以下是(浓缩的)实体:
客户
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
private int addressSourceTypeID = 2;
[NotMapped]
public int AddressSourceTypeID {
get { return addressSourceTypeID; }
set { addressSourceTypeID = value; } }
public virtual ICollection<User> Users { get; set; }
public virtual ICollection<Contract> Contracts { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
合同
public class Contract
{
public int ContractID { get; set; }
public string Name { get; set; }
private int addressSourceTypeID = 4;
[NotMapped]
public int AddressSourceTypeID {
get { return addressSourceTypeID; }
set { addressSourceTypeID = value; } }
public virtual int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
//public virtual ICollection<Address> Addresses { get; set; }
}
地址
public class Address
{
[Key]
public int AddressID { get; set; }
public int AddressSourceTypeID { get; set; }
[ForeignKey("Customer")]
public int SourceKey { get; set; }
public virtual Customer Customer { get; set; }
//public virtual Contract Contract { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
}
我上面有两个实体Customer 和Contract,它们都可以有子Address 实体。目前Address 实体被设置为Customer 实体的子实体,这工作正常,因为没有从Address 到Contract 的链接。
我尝试将Contract 添加到Address 实体中,就像我对Customer 实体所做的那样,正如您从注释掉的代码段中看到的那样。不幸的是,这不起作用,但由于Address ForeignKey 注释中对Customer 的引用,我并不感到惊讶。我什至尝试创建Address 实体的特定版本(即CustomerAddress),但是当多个实体尝试绑定到同一个表时,我得到一个错误。
我也尝试在 EF DBContext 中使用 ModelBuilder,但是我在这里的知识非常有限,我不知道在这种情况下该怎么做。
总的来说,我需要的是:
- 拥有一组子地址的客户实体。
- 合同实体拥有一组子地址。
这些“父”表与地址表之间的链接使用以下内容:
- 客户:CustomerID => 地址:SourceKey AND 客户:AddressSourceTypeID(始终为 2)=> 地址:AddressSourceTypeID。
- 合同:ContractID => 地址:SourceKey AND 合同:AddressSourceTypeID(始终为 4)=> 地址:AddressSourceTypeID。
如果有人可以帮助我或为我指出正确的方向,那就太好了。
非常感谢。
【问题讨论】:
-
数据库中
Address表中的SourceKey列是both一对多关系的外键???这在技术上是允许的,您可以在SourceKey上为Customer和Contract导航属性使用两个[ForeignKey]注释。但这意味着每当Address通过SourceKeyFK 引用Customer“1234”时,也必须有Contract“1234”,反之亦然——这很难想象,因为Customer和Contract以一对多的关系关联,您的密钥看起来像自动生成的身份。
标签: asp.net-mvc-3 sql-server-2008 entity-framework