首页 > 解决方案 > 一对多 MVC 个人用户帐户

问题描述

我将 asp.net mvc 与个人用户帐户模板一起使用,我尝试在其中建立从用户到项目的一对多关系。

在没有个人用户帐户模板的项目中,我可以简单地执行以下操作:

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

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

}

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

    public virtual User User { get; set; }
}

问题是我在个人用户帐户模板中找不到用户 (ApplicationUser) 的完整模型。所以我不知道如何将项目添加到用户并为用户生成控制器(在单个用户模板中)

标签: c#asp.net-mvc

解决方案


从 Models 文件夹中的模板,在 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; }
}

ApplicationUser以这种方式在模型中使用身份关系。您也可以将名称更改ApplicationUserUser此处,前提是所有其他引用ApplicationUser也已更新。


推荐阅读