Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrate existing Microsoft.AspNet.Identity DB (EF 6) to Microsoft.AspNetCore.Identity (EF Core)

I am working on an application (APS.net MVC) which uses Microsoft.AspNet.Identity. Now I want to revamp my application to APS.net Core which uses Microsoft.AspNetCore.Identity. But those two has some differences in each model. Is there any direct way to generate initial migration for Microsoft.AspNetCore.Identity related changes in-order to connect existing DB with Asp.net Core identity?

like image 843
Sampath Avatar asked May 15 '18 06:05

Sampath


3 Answers

I was able to migrate existing DBs using the following steps.

  1. Create a new ASP.net Core project and modify it's ModelSnapshot to match with EF6. After that, you can generate a script for the changes of EF6 to EF Core.

  2. Write a script to update the AspNetUsers table. In ASP.net core identity when authenticating it is used NormalizedEmail, NormalizedUserName columns. So we need to update those two columns using our existing data.

Hope this helps someone out there...

Please follow up this GitHub thread for more information.

like image 193
Sampath Avatar answered Oct 28 '22 20:10

Sampath


There is a thorough explanation here regarding asp.net core 3.0 and how to migrate your db into an asp.net core identity 3.0 db.

Read it through and I hope it will help anyone who comes across it.

like image 41
Barr J Avatar answered Oct 28 '22 19:10

Barr J


I converted a 'full' Identity Database to Core Identity. My situation may not be a perfect fit for yours. I am not using EF Migrations. I only use the Users and Roles features. I used these steps:

  • Copy the database
  • Drop the Identity related tables AND the MigrationHistory...

    • drop table IF EXISTS AspNetRoleClaims
    • drop table IF EXISTS AspNetUserTokens
    • drop table IF EXISTS AspNetUserClaims
    • drop table IF EXISTS AspNetUserRoles
    • drop table IF EXISTS AspNetUserLogins
    • drop table IF EXISTS AspNetUserClaims
    • drop table IF EXISTS AspNetRoles
    • drop table IF EXISTS AspNetUsers
    • drop table IF EXISTS __EFMigrationsHistory

Allow the Identity Core migration to run, creating the new tables that Core Identity prefers

Copy identity data from the Old Database

--Copy over users data

insert into AspNetUsers(Id, UserName, NormalizedUserName, Email, NormalizedEmail,
    EmailConfirmed, PasswordHash, PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount
    )
    select Id, UserName, UPPER(UserName), Email, UPPER(Email), 
    1, PasswordHash, 0, 0, 0, 0 from [OldDatabase].dbo.AspNetUsers

--Copy over Roles

insert into AspNetRoles(Id, [Name], NormalizedName)
     select Id, [Name], UPPER([Name]) from [OldDatabase].dbo.AspNetRoles

--copy over user/roles

insert into AspNetUserRoles select * from [OldDatabase].dbo.AspNetUserRoles

I don't use any of the other tables. I left them empty.

like image 41
RichardHowells Avatar answered Oct 28 '22 20:10

RichardHowells