我遇到了这个问题:
我有这样的应用程序用户类
public class ApplicationUser : IdentityUser
{
public ROLES Role { get; set; }
public int? CompanyId { get; set; }
public int? AreaId { get; set; }
public string Document { get; set; }
public bool Enable { get; set; }
[ForeignKey("CompanyId")]
public virtual Company Company { get; set; }
[ForeignKey("AreaId")]
public virtual Area Area { get; set; }
public virtual ICollection Measures { get; set; }
}
我得到了另一个模型:
public class Area
{
public int AreaId { get; set; }
public string AreaName { get; set; }
public int CompanyId { get; set; }
public string UserId { get; set; }
[ForeignKey("CompanyId")]
public virtual Company Company { get; set; }
[Key, ForeignKey("UserId")]
public ApplicationUser ApplicationUser { get; set; }
}
当我尝试: 添加迁移
PM 控制台抛出:
无法确定类型“x.Models.ApplicationUser”和“x.Models.Area”之间关联的主体端。该关联的主体必须使用关系流畅 API 或数据注释显式配置。
我一整天都在尝试,但我找不到一种方法来告诉实体框架识别这种关系。
有什么想法吗?
感谢您的阅读
在Area类中添加AreaId属性
[Key]
public int AreaId { get; set; }
如果您想要 ApplicationUser 和 Area 建立 1-1 关系,请更新您的代码,如
[Unique]
[ForeignKey("UserId")]
public ApplicationUser ApplicationUser { get; set; }