简体   繁体   English

使用数据注释的实体框架1:0..1(一对​​零或一个)关系

[英]Entity Framework 1:0..1 (One to zero or one) relationship using Data Annotations

Trying to do something that I believe should be simple. 尝试做一些我认为应该很简单的事情。

Customer
------
CustId
BillingId 
ShippingId 
...other customerInfo

Address
------
Id
...other addressinfo

I have the corresponding POCOs 我有相应的POCO

public class Customer {
    [Key]
    public int CustId {get;set;}

    [Column("BillingId")]
    [ForeignKey("BillingAddress")
    public int? BillingId {get;set;}
    public virtual Address BillingAddress{get;set;}

    [Column("ShippingId")]
    [ForeignKey("ShippingAddress")
    public int? ShippingId {get;set;}
    public virtual Address ShippingAddress{get;set;}

    ...others...
}

public class Address {
    [Key]
    public int AddressId{get;set}

    ... others...
}

The BillingId and ShippingId are nullable because the customer may or may not have set an address yet. BillingIdShippingId nullablenullable因为客户可能已经或可能尚未设置地址。 I'd assume with EF that if the values are null, the ShippingAddress and BillingAddress values should also be null . 我假设使用EF,如果这些值为null,则ShippingAddressBillingAddress值也应该为null When I take a look at the object I'm getting back when running the application, all of the data on my Customer object is set but on the ShippingAddress / BillingAddress fields, in debug mode when I inspect the object, I get the below error: 当我查看运行应用程序时要返回的对象时,我的Customer对象上的所有数据都已设置,但在ShippingAddress / BillingAddress字段上,在调试模式下检查对象时,出现以下错误:

BillingAddress = '((System.Data.Entity.DynamicProxies.CustomerD_E865AA67CAAA399D4F193A2439775301DFD2F05115619BC048699B20BF6F7B11)details).BillingAddress' 
threw an exception of type 'System.InvalidOperationException'

A same error appears for the ShippingAddress field. 对于ShippingAddress字段,将出现相同的错误。 The application actually continues to run, the exception only gets thrown when inspecting in debug more. 该应用程序实际上继续运行,只有在调试中进行更多检查时才会抛出异常。 For the particular inspected object, the ShippingId and BillingId are populated correctly. 对于特定的检查对象,将正确填充ShippingIdBillingId

Not sure why this is happening given my setup. 不知道为什么在我的设置下会发生这种情况。

one possible reason is: your repository is registered as singleton in DI configure. 一个可能的原因是:您的存储库在DI configure中注册为单例。

another possible reason: add ForeignKey and InverseProperty to navigation properties 另一个可能的原因:在导航属性中添加ForeignKeyInverseProperty

public class Customer {
    [Key]
    public int CustId {get;set;}

    //[Column("BillingId")]//not necessary if real column is same as property
    public int? BillingId {get;set;}

    [ForeignKey("BillingId")]
    [InverseProperty("Customer")]
    public Address BillingAddress{get;set;} //no need to be virtual

    //[Column("ShippingId")]
    public int? ShippingId {get;set;}

    [ForeignKey("ShippingId")]
    [InverseProperty("Customer")]//InverseProperty: Specifies the inverse of a navigation property that represents the other end of the same relationship.
    public Address ShippingAddress{get;set;} //no need to be virtual

    ...others...
}

try Scaffold-DbContext in a new project to validate your pocos: 在新项目中尝试Scaffold-DbContext来验证您的pocos:

https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext

Scaffold-DbContext "your connection string" -DataAnnotations -OutputDir "Models" -Force

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM