Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get Error when i run enable-migrations in package manager console

I am making a ASP.NET MVC project ...when i type enable-migrations i get the following eroors:

More than one context type was found in the assembly 'eManager.Web'.
To enable migrations for eManager.Web.Infrastructure.DepartmentDb, use Enable-Migrations -ContextTypeName eManager.Web.Infrastructure.DepartmentDb.
To enable migrations for eManager.Web.Models.UsersContext, use Enable-Migrations -ContextTypeName eManager.Web.Models.UsersContext.
like image 400
fizmhd Avatar asked Nov 20 '12 10:11

fizmhd


People also ask

How do I enable migrations in Package Manager console?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations command (make sure that the default project is the project where your context class is).

Why add migration is not working?

Add-Migration - The Term 'Add-Migration' Is Not Recognized After creating models and context class, we nomally add migration to initialize the database. The error occurs sometimes while adding migration in asp.net core, entity framework with code first approach because of some missing library.

How do I enable migrations in Visual Studio?

From the Tools menu, select NuGet Package Manager > Package Manager Console. The enable-migrations command creates a Migrations folder in the ContosoUniversity project, and it puts in that folder a Configuration. cs file that you can edit to configure Migrations.


2 Answers

The error message exactly states what the problem is and what needs to be done - including the command that needs to be issued. Apparently there is more than one context in your project (Web.Infrastructure.DepartmentDb and Web.Models.UsersContext) and migrations does not know for which of these migrations should be enabled. You need to point to the context type. As per the error message use:

Enable-Migrations -ContextTypeName eManager.Web.Infrastructure.DepartmentDb.

to enable migrations for eManager.Web.Infrastructure.DepartmentDb or

Enable-Migrations -ContextTypeName eManager.Web.Models.UsersContext.

to enable migrations for eManager.Web.Models.UsersContext

like image 106
Pawel Avatar answered Oct 05 '22 23:10

Pawel


For those that may want to remain with a single context in the project. In this case, it will be the DepartmentDb context.

Move the below code into your DepartmentDb context:

public DepartmentDb() 
: base("DefaultConnection")
{

}

public DbSet<UserProfile> UserProfiles { get; set; }

Next: Get to your AccountModels.cs and delete/comment out the UsersContext class. You will get build errors - so replace the UsersContext references with DepartmentDb.

Build again and it should succeed.

Now go to the Package Manager Console and run PM> enable-migrations

You should get "Code First Migrations enabled for project eManager.Web."

like image 22
seguya Avatar answered Oct 06 '22 01:10

seguya