我很难映射这种关系。
public class Person
{
public long Id { get; set; }
[InverseProperty("Friend")]
public virtual ICollection<Friendship> Friends { get; set; }
}
public class Friendship
{
public long Id { get; set; }
public long PersonId { get; set; }
[ForeignKey(nameof(PersonId))]
public virtual Person Person { get; set; }
public long FriendPersonId{ get; set; }
[ForeignKey(nameof(FriendPersonId))]
public virtual Person Friend { get; set; }
}
我收到以下错误
The InversePropertyAttribute on property 'Friends' on type 'Person' is not valid. The property 'Friends' is not a valid navigation property on the related type 'Friendship'. Ensure that the property exists and is a valid reference or collection navigation property.
我可以清楚地看到友谊类确实包含一个名为Friend的属性,但我不确定是什么使它成为无效的导航属性。
如果我将InverseProperty
属性更改为FriendPersonId
,则会抛出空引用异常。
答案 0 :(得分:1)
反向属性设置不正确。
应该是[InverseProperty("Person")])
信用:Ivan Stoev指出我的错误。