【发布时间】:2015-06-16 09:38:39
【问题描述】:
我首先使用代码来创建客户管理应用程序。 一个客户可以有多个地址,但只有一个“主要”地址。
这是我的客户模型:
public class Customer
{
[Key]
public int Id { get; set; }
public string FirstName{ get; set; }
public string LastName{ get; set; }
public int MainInvoicingAddressId { get; set; }
[ForeignKey("MainBillingAddressId")]
public Address MainBillingAddress { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
还有我的地址模型:
public class Address
{
[Key]
public int Id { get; set; }
public string Address1 { get; set; }
public int CustomerId { get; set; }
[ForeignKey("CustomerId")]
public virtual Customer Customer { get; set; }
}
但是当我创建数据库时,由于导航属性 MainBillingAddress,我在地址表上有一个自动生成的外键 Customer_Id。
所以在地址表上,我有 2 个客户外键(“CustomerId”和“Customer_Id”)。
我想要的是使用现有外键“CustomerId”与 MainBillingAddress 建立关系。
有可能吗?
【问题讨论】:
标签: c# entity-framework ef-code-first code-first ef-model-first