【发布时间】:2017-02-20 14:52:51
【问题描述】:
我遇到了这个问题:
我有这样的应用程序用户类
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 类中是必需的。尝试在此处使用 Required 属性或使用 fluent API 来指定一个是必需的,另一个是可选的。
标签: entity-framework asp.net-mvc-5 ef-code-first entity-framework-migrations ef-code-first-mapping