.net 实体框架一对一映射
我有一个客户,可以有一个帐单或送货地址。客户拥有与地址表上的地址ID相对应的BillingAddressId和ShippingAddressId 我尝试了以下设置,但不断出现错误:.net 实体框架一对一映射,.net,entity-framework,.net,Entity Framework,我有一个客户,可以有一个帐单或送货地址。客户拥有与地址表上的地址ID相对应的BillingAddressId和ShippingAddressId 我尝试了以下设置,但不断出现错误: public class Customer [Key] public int CustomerId { get; set; } [Column("billingAddressId")] [ForeignKey("Address")] public int BillingAdd
public class Customer
[Key]
public int CustomerId { get; set; }
[Column("billingAddressId")]
[ForeignKey("Address")]
public int BillingAddressId { get; set; }
[Column("shippingAddressId")]
public int ShippingAddressId { get; set; }
public virtual Address BillingAddress { get; set; }
public virtual Address ShippingAddress { get; set; }
地址如下所示:
public class Address
[Key]
public int AddressId {get;set;}
public virtual Customer Customer { get; set; }
我的模型生成器:
modelBuilder.Entity<Customer>()
.HasOptional(c => c.BillingAddress)
.WithOptionalPrincipal(a => a.Customer);
modelBuilder.Entity()
.has可选(c=>c.BillingAddress)
.具有可选本金(a=>a.客户);
但是我一直收到这个错误,
类型“…Customer”的属性“BillingAddressId”上的ForeignKeyAttribute无效。在依赖类型“…Customer
上未找到导航属性“Address”…一对一的可能副本不是使用.hasportional()
所述的“Can-have”关系。您可以考虑在<代码>客户>代码>和<代码>地址>代码>之间有一对多的关系,即使它们只有一个帐单或发货地址。<代码> [外键(“地址”)] /代码>意味着它正在寻找一个名为“代码>地址<代码>的属性,而不是类型<代码>地址<代码>…@ Misteleic我实际上尝试过“地址”。它给了我同样的错误。属性BillingAddress
的FK注释将是[ForeignKey(“BillingAddress”)]
,以匹配相应的属性。