Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 6 Enable-Migrations Can't Find Context

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?

like image 327
A. Smith Avatar asked Dec 30 '15 16:12

A. Smith


People also ask

How do you specify context in migration?

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.

Which command enables migration in EF 6 first?

To use code-based migrations, first execute the enable-migrations command in the Package Manager Console.

Why add migration is not working?

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.


2 Answers

Make sure that you have your default project set to the project with the EF context.

like image 79
Rickest Rick Avatar answered Sep 28 '22 09:09

Rickest Rick


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.

like image 31
A. Smith Avatar answered Sep 28 '22 09:09

A. Smith