CF EF MVC多个1对1外键来自同一个表
本文关键字:同一个 EF MVC 多个 CF | 更新日期: 2023-09-27 18:08:11
我有这个:
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个用户的表(Match)作为外键。
当我尝试添加迁移时,控制台回复:
"类型上的属性'MatchedUser1'的InversePropertyAttribute"H4L.Web.Models。"匹配"无效。属性'User1'不是相关类型上的有效导航属性"H4L.Web.Models.ApplicationUser"。确保该属性存在并且是有效的引用或集合导航属性。"
我不想使用流畅的API,除非它是唯一的方法。
终于想通了。我认为代码只能在这个类中完成,但显然我需要在ApplicationUser类中添加这个:
public virtual ICollection<Match> User1 { get; set; }
public virtual ICollection<Match> User2 { get; set; }