简体   繁体   English

一对多MVC个人用户帐户

[英]One-to-many MVC Individual User Accounts

I am using asp.net mvc with the individual user accounts template where i try to make an one to many relationship from User to Items. 我在个人用户帐户模板中使用asp.net mvc,在该模板中尝试建立从用户到项目的一对多关系。

In a project without the individual user account template i can simply do the following: 在没有个人用户帐户模板的项目中,我可以简单地执行以下操作:

public class User
{
    public int UserID { get; set; }
    public string Username { get; set; }

    public virtual ICollection<Item> Items { get; set; }

}

And

  public class Item
{
    public int ItemID { get; set; }
    public string ItemName { get; set; }
    public int UserID { get; set; }

    public virtual User User { get; set; }
}

Problem is that i cannot find a complete model for the User (ApplicationUser) in the Individual user account template. 问题是我无法在个人用户帐户模板中找到用户(ApplicationUser)的完整模型。 So i dont know how to add the Items to the User and generate a controller for the User (In the individual user template) 所以我不知道如何将项目添加到用户并为用户生成控制器(在单个用户模板中)

From the template in the Models folder, in IdentityModels: 从“模型”文件夹中的模板中,在“ IdentityModels”中:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    // Add here ..
    public string DisplayName { get; set; }
    public virtual ICollection<Item> Items { get; set; }
}

Using Identity ties in the ApplicationUser model this way. 通过这种方式在ApplicationUser模型中使用身份关系。 You can change the name ApplicationUser to User here as well, provided all other references to ApplicationUser are also updated. 您还可以在此处将名称ApplicationUser更改为User ,前提是对ApplicationUser所有其他引用也已更新。

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

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