无法扩展 IdentityUserRole.cs 以将新的组合键添加到 dbo.AspNetUserRoles table ASP.NET Identity 2.1

Can't extend IdentityUserRole.cs to add new composite key to dbo.AspNetUserRoles table ASP.NET Identity 2.1

我正在尝试使用 ASP.NET Identity 2.1、ASP.NET Web API 2.2 构建会员系统,我需要扩展 IdentityUserRole 以在其 table 像那样:

public class ApplicationUserRoles : IdentityUserRole
{
    public Guid ProjectId { get; set; }
    public Project Project { get; set; }
}

public class Project
{
    public Project()
    {
        ProjectId = new Guid();
    }

    public string ProjectName { get; set; }
    [Key]
    public Guid ProjectId { get; set; }
}

并且在 DBcontext class 我添加了:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder); // This needs to go before the other rules!

    modelBuilder.Entity<ApplicationUserRoles>().HasKey(m => new {m.ProjectId, m.UserId, m.RoleId}).ToTable("ApplicationUserRoles");

}

public DbSet<ApplicationUserRoles> ApplicationUserRoles { get; set; }

这根本不起作用,我没有做错什么,添加迁移保持生成:

CreateTable(
        "dbo.ApplicationUserRoles",
        c => new
            {
                UserId = c.String(nullable: false, maxLength: 128),
                RoleId = c.String(nullable: false, maxLength: 128),
                ProjectId = c.Guid(nullable: false),
            })
        .PrimaryKey(t => new { t.UserId, t.RoleId })
        .ForeignKey("dbo.AspNetUserRoles", t => new { t.UserId, t.RoleId })
        .ForeignKey("dbo.Projects", t => t.ProjectId, cascadeDelete: true)
        .Index(t => new { t.UserId, t.RoleId })
        .Index(t => t.ProjectId);

}

如您所见PrimaryKey(t => new { t.UserId, t.RoleId })。我需要它 PrimaryKey(t => new { t.ProjectId,t.UserId, t.RoleId })

我终于成功了,这里是我所做的:

在 IdentityUser 的签名处 myApplicationUser 继承自它

public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
    where TLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
    where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
    where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
{

}

它接受我对 IdentityUserRole 的自定义定义。

我的 class ApplicationUser 我需要实现正确的类型:

public class ApplicationUser : IdentityUser<string, IdentityUserLogin, ApplicationUserSecurityProfile, IdentityUserClaim>
{

}

我的角色:

public class ApplicationSecurityProfile : IdentityRole<string, ApplicationUserSecurityProfile>
{
}

和我的 ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationSecurityProfile, string, IdentityUserLogin, ApplicationUserSecurityProfile, IdentityUserClaim>, IContext
{

}

和我的 UserStore:

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationSecurityProfile, string, IdentityUserLogin, ApplicationUserSecurityProfile, IdentityUserClaim>, IUserStore<ApplicationUser>
{
    public ApplicationUserStore ApplicationDbContext context)
        : base(context)
    {
    }
}

和我的角色商店:

 public class ApplicationSecurityProfileStore : RoleStore<ApplicationSecurityProfile, string, ApplicationUserSecurityProfile>
    {
        public ApplicationSecurityProfileStore(ApplicationDbContext context)
            : base(context)
        {
        }


    }

然后我在自定义管理器中使用了自定义 classes :

角色管理器:

public class ApplicationSecurityProfileManager : RoleManager<ApplicationSecurityProfile>
{
    private ApplicationDbContext db = new ApplicationDbContext();

    public ApplicationSecurityProfileManager(IRoleStore<ApplicationSecurityProfile, string> roleStore)
        : base(roleStore)
    {
    }



public static ApplicationSecurityProfileManager Create(IdentityFactoryOptions<ApplicationSecurityProfileManager> options, IOwinContext context)
    {

        var appRoleManager = new ApplicationSecurityProfileManager(new ApplicationSecurityProfileStore(context.Get<ApplicationDbContext>()));

        return appRoleManager;
    }

}

我的用户管理器

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(ApplicationUserStore store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var appDbContext = context.Get<ApplicationDbContext>();
        var appUserManager = new ApplicationUserManager(new ApplicationUserStore(appDbContext));

        // Configure validation logic for usernames
        appUserManager.UserValidator = new UserValidator<ApplicationUser>(appUserManager)
        {
            AllowOnlyAlphanumericUserNames = true,
            RequireUniqueEmail = true
        };

        // Configure validation logic for passwords
        appUserManager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = false,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        appUserManager.EmailService = new AspNetIdentity.WebApi.Services.EmailService();

        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            appUserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"))
            {
                //Code for email confirmation and reset password life time
                TokenLifespan = TimeSpan.FromHours(6)
            };
        }

        return appUserManager;
    }
}

终于成功了