无法设置 2 个数据库模型之间的关系

Can't set the Relation between 2 DB Models

我遇到了这个问题:

我有这样的应用程序用户Class

 
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 或数据注释显式配置。

我一整天都在尝试,但我找不到告诉 Entity Framework 识别关系的方法。

有什么想法吗?

感谢阅读

为区域中的 AreaId 添加属性 class

[Key]
public int AreaId { get; set; }

如果您想要 ApplicationUser 和 Area 的 1-1 关系,请更新您的代码,例如

[Unique]
[ForeignKey("UserId")]
public ApplicationUser ApplicationUser { get; set; }

The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations

这个Post给我我需要的答案!!! 很难找到...

所以我让你 post 在这里... 感谢大家的帮助!