问题
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?
回答1:
I was able to migrate existing DBs using the following steps.
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.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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/50343512/migrate-existing-microsoft-aspnet-identity-db-ef-6-to-microsoft-aspnetcore-ide