简体   繁体   English

CF EF MVC来自同一表的多个一对一外键

[英]CF EF MVC multiple 1-to-1 foreign key from same table

I have this: 我有这个:

public class Match
{
    [Key]
    public string Id { get; set; }
    [ForeignKey("MatchedUser1")]
    public string IdOf1 { get; set; }
    [ForeignKey("MatchedUser2")]
    public string IdOf2 { get; set; }

    [InverseProperty("User1")]
    public virtual ApplicationUser MatchedUser1 { get; set; }
    [InverseProperty("User2")]
    public virtual ApplicationUser MatchedUser2 { get; set; }

    public List<ApplicationUser> User1 { get; set; }
    public List<ApplicationUser> User2 { get; set; }

    public virtual ApplicationUser User { get; set; }
}

The idea is to have a table (Match) containing 2 users as foreign keys. 这个想法是要有一个包含2个用户作为外键的表(匹配)。

When I try to add the migration the console replies: 当我尝试添加迁移时,控制台会回复:

"The InversePropertyAttribute on property 'MatchedUser1' on type 'H4L.Web.Models.Match' is not valid. The property 'User1' is not a valid navigation property on the related type 'H4L.Web.Models.ApplicationUser'. Ensure that the property exists and is a valid reference or collection navigation property." “类型'H4L.Web.Models.Match'上属性'MatchedUser1'的InversePropertyAttribute无效。属性'User1'在相关类型'H4L.Web.Models.ApplicationUser'上不是有效的导航属性。请确保该属性存在,并且是有效的引用或集合导航属性。”

And I don't want to use fluent API unless it's the only way. 而且,除非这是唯一的方法,否则我不想使用流畅的API。

Finally figured it out. 终于想通了。 I thought the code had to be done solely in this class but apparently I needed to add this in the ApplicationUser class: 我认为代码必须仅在此类中完成,但是显然我需要在ApplicationUser类中添加此代码:

public virtual ICollection<Match> User1 { get; set; }
public virtual ICollection<Match> User2 { get; set; }

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

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