cembo
cembo

Reputation: 1137

Migration: No DbContext was found in assembly

Using VS Community 2017. I have tried to create initial migration with error message saying:

Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework\Add-Migration' for Entity Framework 6. No DbContext was found in assembly 'Test_Project'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

... code in my dbcontext:

protected override void OnModelCreating(DbModelBuilder mb)
{
    base.OnModelCreating(mb);

    mb.Entity<Stuff>().ToTable("Stuff");

}

public DbSet<Stuff> Stuff{ get; set; }

Upvotes: 44

Views: 106810

Answers (9)

maneesh
maneesh

Reputation: 1147

Try this, you have to specify your context

Syntax :

Enable -Migrations -Context MyDbContext please note : use -ContextTypeName (Entity Framework 6.x and Earlier):

example :

Enable -Migrations -Context CoreMVCWebApphotelMgmnt.Infrastructure.Data.ApplicationDBContext

enter image description here

Upvotes: 0

NKAZIMULO PANWELL
NKAZIMULO PANWELL

Reputation: 1

I was receiving the same error message see below. PM> Add-Migration "Initial Migration" Both Entity Framework 6 and Entity Framework Core are installed. The Entity Framework 6 tools are running. Use 'EntityFrameworkCore\Add-Migration' for Entity Framework Core. No migrations configuration type was found in the assembly 'CodePulse.API'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).

If you ready the error message carefully it is self explanatory and the solution is there. All you have to do is run this command EntityFrameworkCore\Add-Migration YourMigrationName

Upvotes: 0

Vamsi J
Vamsi J

Reputation: 639

In the Package Manager Console, select the Project where the class that inherits DbContext is defined. Then, run the command add-migration initial.

Upvotes: 44

Yes, just close the visual studio and re-open and execute the command, If you are using PowerShell for migration use below command.

dotnet ef migrations add Initial

If you need to reset the database

dotnet ef database drop --force --context DbContext_ClassName

Upvotes: -2

Wiff1
Wiff1

Reputation: 301

You have to specify the project name where the DbContext is located. So just right on the NuGet PM Console, type: Add-Migration MigrationName -Project YourProjectName.

Upvotes: 30

Ololary
Ololary

Reputation: 11

I solved the problem by renaming the project, before that it coincided with the name of the solution.

Upvotes: 1

Angad Narang
Angad Narang

Reputation: 9

Just close the visual studio and restart your project and then write command again. It worked for me twice

Upvotes: 0

Ricomyer
Ricomyer

Reputation: 1

When I had this issue, I had just renamed an existing project to "EF", then renamed all namespaces to "EF". What worked for me was I changed the Assembly name to something other than "EF" (Project properties/Application/Assembly Name). I was using VS Community 2019.

Upvotes: 0

Chris
Chris

Reputation: 43

Using the nuget package manager, I had installed all the EntityFrameworkCore dependencies, along with the EntityFramework 6 dependency. Visual Studio was using EF Core, when I needed it to be using EF 6. Uninstalling all EF Core dependencies resolved this issue.

Upvotes: 2

Related Questions