I am trying to set up EF code first migrations in EF 6.1.3 - .NET 4.5.
My solution has multiple projects in it, the startup project being Songbirds.Web
. I have created a Project named Songbirds.Dal.EntityFramework
to contain my repositories, database context, and migrations.
I created my context class:
namespace Songbirds.Dal.EntityFramework
{
public class SongbirdsDbContext : IdentityDbContext<ApplicationUser>, IUnitOfWork
{
public SongbirdsDbContext()
: this("name=SongbirdsDBContext")
{
}
...
}
}
The entire solution builds properly with no errors.
I go into the Project Manager Console and set the Default Project to be the Songbirds.Dal.EntityFramework
and run the enable-migrations
command and I get the following error:
PM> enable-migrations
No context type was found in the assembly 'Songbirds.Dal.EntityFramework'.
I tried specifying the Context Type explicitly with the following result:
PM> enable-migrations -ContextTypeName Songbirds.Dal.EntityFramework.SongbirdsDbContext
The context type 'Songbirds.Dal.EntityFramework.SongbirdsDbContext' was not found in the assembly 'Songbirds.Dal.EntityFramework'.
The SongbirdsDbContext is part of the Songbirds.Dal.EntityFramework project. Any ideas what I've done wrong and why it isn't recognizing the context?
Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.
To use code-based migrations, first execute the enable-migrations command in the Package Manager Console.
add-migration : The term 'add-migration' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Make sure that you have your default project set to the project with the EF context.
I think I found the answer through trial and error. I first changed the context class to inherit from the DbContext class instead of IdentifyDbContext:
public class SongbirdsDbContext : DbContext
And re-ran the enable-migrations
command to find the following error:
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.
After adding the appropriate reference to the required assembly, I was able to successfully enable migrations. I'm not sure why inheriting from DbContext showed this error while inheriting from IdentityDbContext did not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With