2015-08-15 23 views
0

外键我有这样的:CF EF MVC多个1对1从同一表

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; } 
} 

的想法是具有包含2个用户作为外键的表(匹配)。

当我尝试添加迁移控制台回复:

“的InversePropertyAttribute物业‘MatchedUser1’在类型 ‘H4L.Web.Models.Match’是无效的财产‘用户1’。是不是对相关类型 “H4L.Web.Models.ApplicationUser”一个 有效的导航属性。确保属性存在和 是一个有效的参考或集合导航属性“。

我不想使用流利的API,除非它是唯一的方法。

回答

0

终于想通了。 我认为代码只能在这个类中完成,但显然我需要将它添加到ApplicationUser类中:

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