38

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; }
cembo
  • 1,039
  • 2
  • 9
  • 18
  • Are you using ASP.NET Core MVC with EF Core, also with `dotnet ef migrations add Initial`/`dotnet ef database update` command instead of `Add-Migration`/`Update-Database` in PM console? There are some missing points you need to explain here. – Tetsuya Yamamoto Oct 11 '17 at 03:21
  • It's a MVC project using EF Core. I tried "dotnet ef migrations add" but gave me message: "dotnet : No executable found matching command "dotnet-ef"" – cembo Oct 11 '17 at 03:59
  • 2
    Well, I think you can look for this first: https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations. If you have EF 6 with Core MVC try to use EF Core & remove EF 6 references. – Tetsuya Yamamoto Oct 11 '17 at 04:04
  • 1
    "Both Entity Framework Core and Entity Framework 6 are installed." - is that on purpose? Because I would go out of my way to avoid that. – H H Oct 11 '17 at 04:18
  • 1
    Ok, I have uninstalled EF Core keeping only EF 6. Now after I ran 'enable-migrations' and 'add-migration Initial' all works. Thanks guys. – cembo Oct 11 '17 at 07:39
  • 1
    I used 'EntityFramework\Add-Migration' and 'EntityFramework\Update-Database' when I got this error message, but I guess like Henk Holterman says the two framework should not be installed at one time... – Liknes Apr 26 '18 at 23:11

7 Answers7

40

In the Package Manager Console select the project where the DbContext is defined and run the command add-migration initial. For example:public class SomeContext : DbContext

Little Endian
  • 784
  • 8
  • 19
Vamsi J
  • 601
  • 6
  • 8
25

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

Wiff1
  • 251
  • 3
  • 2
  • 2
    This answer helped me because I am working in a solution that contains multiple projects. Thanks. – Mifo Jul 18 '20 at 14:14
  • 1
    I am also working on a solution that contains multiple projects, and I use this answer with `update-database -Project YourProjectName` and it works like a charm. – Merna Mustafa Sep 21 '21 at 21:11
  • Thanks, worked for me. In my case the starter project was .Api and other project was .Core . I had all the migration and database stuff in Core. I Was getting error like : No DbContext was found in assembly '.Api'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic. I finally used the commands: Add-Migration MigrationName -Project YourProjectName. update-database -Project YourProjectName And it worked like a charm. – iprashant Jan 08 '22 at 13:54
  • Worked a treat for me Visual Studio 2022 Preview .NET6 EF 6.0.0 – Jiving Rockabilly Apr 04 '22 at 14:01
2

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.

Chris
  • 43
  • 7
0

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.

0

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

0

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

Ololary
  • 1
  • 1
0

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