How to integrate ASP.NET Identity tables in an existing database

asp.net-mvc entity-framework entity-framework-6 sql-server

Question

Using Asp.net 4.5 and VS 2013, I would like to know how to integrate Asp.Net Identity Tables with an existing database.

So basically I would like to have db tables for Identity and my own tables in the same db.

I know Asp.Net Identity use Code First approach but I would like to use DB first approach for my existing db. I am little confuse.

How would you handle this?

Does it make sense keep Asp.Net Identity Tables in a different database that my own Tables?

Accepted Answer

I think that depends preety much on what you want to achieve.

Is it a single asp.Net application that you are developing or a set of different apps that would use the same database?

Here there is an answer I pretty much agree with. Separate schema and one database for one app/database. Two databases kind of beat the point of separation but three databases might be a better solution if the authorization rules remain the same.

As for the DB first approach there is a pretty good article (and code) here.


Popular Answer

Edit your identitymodel to override model creation and point to your existing tables you have set-up

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

        modelBuilder.Entity<IdentityUser>().ToTable("Users");
        modelBuilder.Entity<ApplicationUser>().ToTable("Users");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
    }



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why