【问题标题】:ASP.NET MVC Identity RelationshipASP.NET MVC 身份关系
【发布时间】:2015-08-03 11:26:02
【问题描述】:

我是 ASP.NET MVC 4 的新手,我正在开发一个应用程序。在我的应用程序中,我有一个带有属性 ID、名称的地区表。我想将此区表与 identityModel.cs 文件中的 applicationUser 相关联。我的主要目标是在我的用户和地区之间建立一对多的关系。 例如,一个以上的用户映射到一个地区。我在我的 applicationUser 类中试过这个

public class ApplicationUser : IdentityUser
{
    public int ApplicationUserID { get; set; }

    [ForeignKey("District")]
    public int DistrictID { get; set; }
    public virtual District District { get; set; }
}

这也是我的地区表

public class District
{
    public int DistrictID { get; set; }

    [Required]
    [Display(Name="District Name")]
    public string Name { get; set; }

    public string Status { get; set; }

    //Retrieving the region value through the id
    [Display(Name = "Region")]
    public int RegionID { get; set; }
    public virtual Region Region { get; set; }

    public virtual List<AreaCouncil> AreaCouncils { get; set; }

    public virtual List<ApplicationUser> ApplicationUsers { get; set; }
}

当我尝试搭建脚手架时,我收到以下错误消息:

'无法检索 'Basis.Models.Region' 的元数据。在模型生成期间检测到一个或多个验证错误。 Basis.Models.IdentityUserLogin::Entity Type 'IdentityUserLogin' 没有>定义的键。定义此实体类型的键。 Basis.Models.IdentityUserRole::Entity Type 'IdentityUserRole' 没有>定义的键。定义此实体类型的键。'........

谁能帮我完成这段关系?

【问题讨论】:

    标签: c# entity-framework asp.net-mvc-4


    【解决方案1】:

    在 ApplicationUser 中,您只需要 District 的属性。除非你真的想要运行时多态性,否则你不想让它成为虚拟的。

    我会将该属性命名为 District 以外的其他名称。例如

    public District HomeDistrict {get;set}
    

    【讨论】:

      【解决方案2】:

      IdentityUser 已经有 PK。删除

      public int ApplicationUserID { get; set; }
      

      来自您的ApplicationUser 班级

      【讨论】:

        猜你喜欢
        • 2010-09-24
        • 1970-01-01
        • 2016-05-21
        • 2014-04-30
        • 1970-01-01
        • 1970-01-01
        • 2013-09-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多