简体   繁体   English

ASP.NET MVC身份关系

[英]ASP.NET MVC Identity Relationship

I am new to ASP.NET MVC 4 and i'm developing an application. 我是ASP.NET MVC 4的新手,正在开发一个应用程序。 In my application i have a districts table with properties id, name. 在我的应用程序中,我有一个带有属性ID,名称的Districts表。 i want to associate this district table to applicationUser in the identityModel.cs file. 我想将此分区表与identityModel.cs文件中的applicationUser关联。 My main goal is to establish a one to many relationship between my users and districts. 我的主要目标是在用户和地区之间建立一对多的关系。 As in, a more than one user maps to a single district. 与之类似,一个以上的用户映射到单个区域。 i tried this in my applicationUser class 我在我的applicationUser类中尝试过

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

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

this is my districts table too 这也是我的地区表

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; }
}

When i try to scaffold, i receive the following error message: 当我尝试脚手架时,我收到以下错误消息:

'Unable to retrieve metadata for 'Basis.Models.Region'. '无法检索'Basis.Models.Region'的元数据。 One or more validation >errors were detected during model generation. 在模型生成期间检测到一个或多个验证错误。 Basis.Models.IdentityUserLogin::Entity Type 'IdentityUserLogin' has no key >defined. Basis.Models.IdentityUserLogin :: Entity Type'IdentityUserLogin'没有定义的键。 Define the key for this Entity Type. 定义此实体类型的键。 Basis.Models.IdentityUserRole::Entity Type 'IdentityUserRole' has no key >defined. Basis.Models.IdentityUserRole :: Entity Type'IdentityUserRole'没有定义的键。 Define the key for this Entity Type.'........ 定义此实体类型的键。

Can anyone please help me out in accomplishing this relationship? 任何人都可以帮我完成这种关系吗?

In ApplicationUser you just want a property of District. 在ApplicationUser中,您只需要District的属性。 You don't want to have it virtual unless you actually want runtime polymorphism. 除非您确实想要运行时多态性,否则您不希望将其虚拟化。

I would name the property something else than District. 除了区之外,我还要给酒店命名。 Eg 例如

public District HomeDistrict {get;set}

IdentityUser already has a PK. IdentityUser已经有一个PK。 remove 去掉

public int ApplicationUserID { get; set; }

from your ApplicationUser Class 从您的ApplicationUser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM