EF6:2个FK引用的错误消息

本文关键字:错误 消息 引用 FK 2个 EF6 | 更新日期: 2023-09-27 18:00:02

我首先使用数据库。

我得到错误:

保存不公开其关系的外键属性的实体时出错。EntityEntries属性将返回null,因为无法将单个实体标识为异常的源。通过在实体类型中公开外键属性,可以更容易地在保存时处理异常。

我的表有两个相同的对象(本例中为地址)。1为空,1不是。我尝试了在类似的帖子中找到的建议(在数据库中使FK-Ids可以为null),但没有用。

表FK为:ResidenceAddressId、MailingAddressId

FK与表"地址"的关系

类被称为"地址"

主要对象具有以下属性:

公共地址住宅地址{get;set;}

公共地址MailingAddress{get;set;}

我想我需要做一些映射,但我不知道在哪里/如何。

EF6:2个FK引用的错误消息

将其添加到您的对象中:

public int ResidenceAddressId { get; set; }
public int MailingAddressId { get; set; }

然后添加注释:

[ForeignKey("ResidenceAddressId")]
public Address ResidenceAddress { get; set; }
[ForeignKey("MailingAddressId")]
public Address MailingAddress { get; set; }