ASP.Net Identity - Use custom Schema

后端 未结 3 1331
一整个雨季
一整个雨季 2020-12-05 07:57

I am using MVC5 + Ef6 code first with ASP.Net Identity 1.0 and wish to have the tables created in a custom schema. i.e. a schema that is not the dbo schema.

I revers

3条回答
  •  一整个雨季
    2020-12-05 08:46

    Sorry my english, i use google translator.

    Some steps indicated by Prisioner ZERO are not necessary. The indications provided are based on the standard template with individual user accounts security.

    First we must verify that our project is clean (insert commands in Package Management Console):

    1. If you already have a database created with the default ASP.NET Identity schema, you have to delete the database with the following command (or delete directly in SQL Server) :

    Drop-Database

    1. If you have the default migration of the ASP.NET Identity template, execute the following command to remove it:

    Remove-Migration

    Now that our project is clean, we must modify the ApplicationDbContext class. We must overwrite the method OnModelCreating to indicate the scheme to which each of the tables generated by ASP.NET Identity will belong. The following link shows the entities used to map each of the tables as well as information about custom builders and options to change the data type of the primary key of each table: Identity Model Customization.

    public class ApplicationDbContext : IdentityDbContext {
        public ApplicationDbContext(DbContextOptions options) : base(options) { }
    
        protected override void OnModelCreating(ModelBuilder builder) {
            base.OnModelCreating(builder);
            builder.Entity().ToTable("AspNetUsers", "myschema");
            builder.Entity().ToTable("AspNetRoles", "myschema");
            builder.Entity().ToTable("AspNetUserClaims", "myschema");
            builder.Entity().ToTable("AspNetUserRoles", "myschema");
            builder.Entity().ToTable("AspNetUserLogins", "myschema");
            builder.Entity().ToTable("AspNetRoleClaims", "myschema");
            builder.Entity().ToTable("AspNetUserTokens", "myschema");
        }
    }
    

    Now we only have to generate our migration. For this in the Package Management Console enter the following command (optionally you can indicate the output route with the -OutputDir parameter):

    Add-Migration InitialSchemaIdentity -OutputDir Data\Migrations

    Then we apply the changes in our database with the command:

    Update-Database

提交回复
热议问题