Trying to change Identity table names Asp.net MVC5

I want to change the default ap.net identity table names. I have few questions in my head.

1) Will it be possible if already created tables and some data

2) Can we just change the table names and keep the data as is

I tried this code:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<IdentityUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
    modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
    modelBuilder.Entity<IdentityUserRole>().ToTable("MyUserRoles");
    modelBuilder.Entity<IdentityUserLogin>().ToTable("MyUserLogins");
    modelBuilder.Entity<IdentityUserClaim>().ToTable("MyUserClaims");
    modelBuilder.Entity<IdentityRole>().ToTable("MyRoles");
}

      

but it throws me the context support model changed the error

The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database

      

+3


source to share


2 answers


As the error message says, you must enable Migration

DbContext for the Asp.Net Identity Application to reflect the changes in the database. To start using Migrations with your project, go to the tools-> menu in Visual Studio and select Library Package Manager → Package Manager. When the console opens at the bottom of the screen, enter:

Enable-Migrations –EnableAutomaticMigrations

      

Once we run the Enable-Migrations command as above, there should be a Migrations folder at the root of our project. If we open the Configuration.cs file in this folder:



namespace DbMigrationExample.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration 
        : DbMigrationsConfiguration<DbMigrationExample.Models.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(
            DbMigrationExample.Models.ApplicationDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

      

for more information see the article below: Code Porting and Extending Identity Accounts in ASP.NET MVC 5 and Visual Studio 2013

0


source


You can disable initializers. I turned it off as I prefer to maintain my database and code. For example.

Database.SetInitializer<Entities>( null );

      



You can also keep the model names as they are if you want, and specify the table name explicitly in the model, for example:

[Table( "NewTableName" )]
internal class OldTableName
{
    ....
}

      

0


source







All Articles