无法设置2个DB模型之间的关系

问题描述 投票:0回答:2

我遇到了这个问题:

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

 
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 asp.net-mvc-5 ef-code-first entity-framework-migrations ef-code-first-mapping
2个回答
0
投票

在Area类中添加AreaId属性

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

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

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

0
投票

此关联的主体必须使用关系流畅 API 或数据注释显式配置

这篇文章给了我我需要的答案! 好难找啊...

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

© www.soinside.com 2019 - 2024. All rights reserved.