Dan
Dan

Reputation: 573

IdentityDbContext how to configure migrations

Very similar to IdentityDbContext how to run migrations, but nothing there seemed to solve my issues. I've done my research, and there doesn't seem to be much info out there on using IdentityDbContext with migrations.

Some background... I inherited a web app that uses Entity Framework (which I'm not really that familiar with). I wasn't sure what libraries the packages.config was missing. I eventually got that sorted and found it used Entity Framework. I currently have it set to use 6.2.0 and the app seems to work fine.

The original developer had implemented IdentityDbContext and was using automatic migrations. He also had a DbMigrationsConfiguration implementation already defined, which I've added some seed queries to.

I've been using the automatic migrations so far, but I know it's not recommended for production. And I've had a couple times where I had to enable data loss to get the app to deploy, so that's probably why I shouldn't be using it.

This led me to Code First Migrations with an existing database, specifically to set up migrations for an existing schema. That's where I'm lost. It doesn't seem like any of those instructions work for IdentityDbContext. Here's what I've tried:

PM> Enable-Migrations
No context type was found in the assembly 'WebApp'.

PM> Enable-Migrations -ProjectName DataLayer
No context type was found in the assembly 'DataLayer'.

So, this led me to questioning whether IdentityDbContext would even work. So, next I tried changing my context to inherit from DbContext, which also meant having to add DbSet properties for users and roles so that my code would compile (though I never tested to see whether the app still worked). Now, here's what I tried:

PM> Enable-Migrations -ProjectName DataLayer
Migrations have already been enabled in project 'DataLayer'. To overwrite 
the existing migrations configuration, use the -Force parameter.

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
No migrations configuration type was found in the assembly 'DataLayer'. (In 
Visual Studio you can use the Enable-Migrations command from Package Manager 
Console to add a migrations configuration).

Am I reading this right? It can find a migrations configuration, but then it can't? Does anyone test these tools?

Ok, so then I decide to take the recommendation of using -Force:

PM> Enable-Migrations -ProjectName DataLayer -Force
Code First Migrations enabled for project DataLayer.

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
The project 'WebApp' failed to build.

And, as it says, the project failed to build because my existing DbMigrationsConfiguration implementation got overwritten. My existing implementation had a constructor that took some data for the seed queries. So, just for testing I decided not to worry about those seed queries, so I kept the auto-generated configuration and updated my app code to compensate. Now on to creating the initial migration:

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
System.IO.FileNotFoundException: Could not load file or assembly 
'Microsoft.AspNet.Identity.Core, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system 
cannot find the file specified.
File name: 'Microsoft.AspNet.Identity.Core, Version=2.0.0.0, 
Culture=neutral, PublicKeyToken=31bf3856ad364e35'

So, I already had Microsoft.AspNet.Identity.Core 2.2.1 installed to DataLayer through NuGet, but apparently it wasn't added as a reference in the DataLayer project. Added it in and let's try yet again:

PM> Add-Migration InitialCreate –IgnoreChanges -ProjectName DataLayer
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more 
validation errors were detected during model generation:

DataLayer.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key 
defined. Define the key for this EntityType.
DataLayer.IdentityUserRole: : EntityType 'IdentityUserRole' has no key 
defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on 
type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on 
type 'IdentityUserRole' that has no keys defined.

I assume these errors have to do with me switching IdentityDbContext to DbContext.

I don't really know where to go from here and I'm kind of frustrated. It really doesn't seem like this is going to work. If I really do have to change my IdentityDbContext over to a DbContext every time I add a migration, that's just unacceptable.

Even if I did get this to work, it seems convoluted when it's easy to write straight SQL DDL scripts. I suppose the advantage is migrations cover multiple versions of upgrades, but that can be handled with DDL scripts and a version number table, so that's not really an issue either.

Should I just try to give up using migrations? Or maybe Entity Framework completely?

Edit:

I found No context type was found in the assembly EF6 which explained why IdentityDbContext wasn't working. Apparently Microsoft.AspNet.Identity.Core doesn't get added as a reference, which seemed odd because I was kind of expecting the reference to be added with NuGet. But, that only solves the Enable-Migrations issue. Still has the no migration configuration error on Add-Migration.

Upvotes: 2

Views: 2055

Answers (1)

Dan
Dan

Reputation: 573

Well, I think I have it all fixed. So, on top of the Enable-Migrations tool apparently swallowing missing references instead of telling you about them, the Add-Migration tool apparently needs -ConfigurationTypeName in order to find the configuration. Not entirely sure why, since Enable-Migrations was able to find the configuration without this. But as long as it works, that's all I care about.

Upvotes: 1

Related Questions