简体   繁体   中英

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.

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. 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:

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. You can change the name ApplicationUser to User here as well, provided all other references to ApplicationUser are also updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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