Trying to change table names in ASP.NET Identity 2.0

后端 未结 2 813
谎友^
谎友^ 2020-12-09 12:20

I want to change the names of the tables used by ASP.NET Identity 2.0. I\'ve seen various similar questions but nothing that solves my problem. For instance this type of sol

2条回答
  •  鱼传尺愫
    2020-12-09 13:20

    Few steps to follow:

    • Install NuGet Package: Microsoft.AspNet.Identity.EntityFramework
    • Add a connection string to your web.config/app.config

    Now you have to define your custom Database Context:

    public class MyContext : IdentityDbContext
    {
        public MyContext()
            : base()
        {
    
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity()
                .ToTable("Users");
    
            modelBuilder.Entity()
                .ToTable("Roles");
    
            modelBuilder.Entity()
                .ToTable("UserRoles");
    
            modelBuilder.Entity()
                .ToTable("UserClaims");
    
            modelBuilder.Entity()
                .ToTable("UserLogins");
        }
    }
    

    As you can see I've used DbModelBuilder to map all the entities to a new tables.

    • Open NuGet Package Manager Console
    • Execute command Enable-Migrations

    It will create a folder Migrations with the configuration file Configuration.cs.

    It should look something like this:

    internal sealed class Configuration : DbMigrationsConfiguration
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }
    
        protected override void Seed(ConsoleApplication1.Models.MyContext context)
        {
    
        }
    }
    

    In the constructor change the property AutomaticMigrationsEnabled to true.

    • Open NuGet Package Manager Console
    • Execute command Update-Database

    It should run the script to create the new tables.

    You can customize your entities (and Ids) creating custom class for each interface defined.

    public class MyUser : IdentityUser
    {
    }
    

    Since you're using Owin you can define your UserStore:

    public class MyUserStore: UserStore
    {
        public MyUserStore(MyContext context)
            : base(context)
        {
        }
    }
    

    and your UserManager implementation:

    public class ApplicationUserManager : UserManager
    {
        public ApplicationUserManager(IUserStore store)
            : base(store)
        {
    
        }
    
        public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new MyUserStore(context.Get()));
    
            manager.UserValidator = new UserValidator(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
    
            manager.PasswordValidator = new PasswordValidator()
            {
                RequiredLength = 5,
                RequireNonLetterOrDigit = false,     // true
                // RequireDigit = true,
                RequireLowercase = false,
                RequireUppercase = false,
            };
    
            return (manager);
        }
    }
    

    And your Owin.Startup should look something like this:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.CreatePerOwinContext(MyContext.Create);
            app.CreatePerOwinContext(ApplicationUserManager.Create);
        }
    }
    

    If you want to have a look at a custom implementation you can check my GitHub repository with a simple working solution on ASP.NET MVC.

    UPDATE:

    There's another project in that solution with a minimal setup; basically you only need to define your context (IdentityDbContext) if you only want to rename your tables.

    The project can be found here.

提交回复
热议问题