Aspnetcore.docs: Generate Asp.net core initial migration for exisitng DB which was used by Asp.net MVC app

Created on 15 May 2018  Â·  27Comments  Â·  Source: dotnet/AspNetCore.Docs

I have a existing Asp.net MVC application. In-order to convert it to a Asp.net Core application, how do I generate DB migration. What I need is upgrade ASP.NET identity tables to ASP.NET Core identity.

EDIT: 7.4K PV <-- Serious customers


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P1 PU Source - Docs.ms doc-enhancement

Most helpful comment

Migration.zip

These are the migration scripts I generated. Feel free to modify them accrding to your requirement.

All 27 comments

Hi @scottaddie, My current application is having Microsoft.AspNet.Identity (Asp.net MVC). I want to use that DB with Microsoft.AspNetCore.Identity.

@sampathdr Thank you for the clarification.

@blowdart Do you have any resources for migrating Identity tables from ASP.NET 4.x to ASP.NET Core? This detail appears to be missing from our migration guide.

Nope, not a thing. @HaoK ?

@ajcvickers are there any general EF 6 -> EF Core migration docs that we can link to here? I don't think we need anything special for the identity migration

@HaoK Since some properties are different in Microsoft.AspNet.Identity (EF6) and Microsoft.AspNetCore.Identity (EF Core) we cannot directly apply 00000000000000_CreateIdentitySchema migration on top of current DB. So I think we may need to create a migration manually. Is there any better way to handle this?

Ping. I am also looking for guidance on how to do this. I have several ASP.NET Identity (EF6) databases that I'd like to migrate since we are moving everything to ASP.NET Core.

@jessebarocio 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 chnages 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.

+1 for official guidance on migrating the db as well.

@sampathdr Any chance you can share the modified ModelSnapshot?

@nguyenquyhy Yes, I can share ModelSnapshot and generated scripts with you. I will send an email to your email.

I have an existing Asp.net MVC application with EF6 and an IdentityDbContext. ( Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext )

Is there any way I can use the same database from a .core project that is using EF core.

It seems like there is a lack of compatibility that will force me either remain on the full framework or convert all projects (no budget for this) that use the identity db at the same time.

@sampathdr I'm in the exact same situation. Would you be willing to share your ModelSnapshot and scripts with me as well?

@sampathdr I'm in the exact same situation. Would you be willing to share your ModelSnapshot and scripts with me as well?

Hi Jake, I have sent you an email with DB scripts.

+1 @sampathdr

Maybe add as a comment to this thread?

+1 @sampathdr
Can you add those scripts here as comment? Thanks

Migration.zip

These are the migration scripts I generated. Feel free to modify them accrding to your requirement.

I was able to update the schema, however I still had to migrate existing users to use the new password hasher algorithm. The easier (probably safer) option is to ask all users to reset their passwords.

If you don't want to ask users for resetting their passwords, but want to gradually update password hashes to the new algorithm as they authenticate, I posted what I did in this Stack Overflow question.

@AlissonRS Hello, Alisson!
When I've been converted one of the projects to use Identity 3 instead of 2nd version, I've done some research about passwords hashes difference and operations compatibility.
I found that implementations of UserManager methods (that are related to the passwords verification) in Identity 3 are able to determine if hashes has been generated by old Identity 2. This is done via so called _format marker_ anaslysis-- the first byte of password's hash. And based on this verification can use either old hashing strategy or new one, no rehashing required.
The same thing is described in docs of CompatibilityMode option of hasher.

So if it's not too late and if prior to Identity 3 you've used default hashing strategy, then I'd suggest you to consider try these compatibility features instead of writing them by hand and force users to reset passwords. It worked well in project which I've converted to use new Identity.
Hope that this is helpful.

Hi @CallStackRunner

Thanks for your reply. I'm aware of this feature, in fact I tried it, but I think it only works when migrating between .Net Core projects, I tried using this with users created in MVC 5 project and it didn't work.

My guess is that this only works for users created using the Microsoft.AspNetCore.Identity package, which was not the case for my MVC 5 project.

I appreciate your reply.

@AlissonRS That's strange. "MVC5 --> Core" conversion was my case (Identity conversion is only a part of it) and users has been created using old packages of Identity.

Well, hopefully this might be helpful for anyone else who will face this task :)

@CallStackRunner I'll give another try just in case, maybe I did something wrong. I'll post the outcome here.

@CallStackRunner when I use CompatibilityMode without overriding the PasswordHasher, the login works for existing users, but the hashes are not updated to newest version. Have you faced this situation? I'd like hashes to be rehashed to latest algorithm version.

@AlissonRS I don't think that default behavior is to update hash, so it's expected that they aren't updated.
In my case I didn't tried rehashing at all, backward compatibility was enough for my requirements.
According to this article, the supposed way to do this is exactly to implement custom PasswordHasher and return PasswordVerificationResult.SuccessRehashNeeded when necessary.
So if you need to rehash passwords then you're on the right way :)

@Rick-Anderson Is there something specific you want me to address here?

@HaoK asked

@ajcvickers are there any general EF 6 -> EF Core migration docs that we can link to here? I don't think we need anything special for the identity migration

If not, I'll close this issue.

@Rick-Anderson We currently have this: https://docs.microsoft.com/en-us/ef/efcore-and-ef6/porting/index

Plus an issue to make it better: https://github.com/aspnet/EntityFramework.Docs/issues/1180

So closing here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danroth27 picture danroth27  Â·  3Comments

wgutierrezr picture wgutierrezr  Â·  3Comments

davisnw picture davisnw  Â·  3Comments

AnthonyMastrean picture AnthonyMastrean  Â·  3Comments

aaron-bozit picture aaron-bozit  Â·  3Comments