【问题标题】:CF EF MVC multiple 1-to-1 foreign key from same tableCF EF MVC 来自同一个表的多个 1 对 1 外键
【发布时间】:2015-11-08 21:30:19
【问题描述】:

我有这个:

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 个用户作为外键的表(匹配)。

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

“类型上的属性 'MatchedUser1' 的 InversePropertyAttribute “H4L.Web.Models.Match”无效。属性“User1”不是 相关类型的有效导航属性 'H4L.Web.Models.ApplicationUser'。确保该属性存在并且 是一个有效的引用或集合导航属性。”

而且我不想使用 fluent API,除非它是唯一的方法。

【问题讨论】:

    标签: c# entity-framework model-view-controller code-first


    【解决方案1】:

    终于明白了。 我认为代码必须仅在此类中完成,但显然我需要将其添加到 ApplicationUser 类中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      相关资源
      最近更新 更多