17

I have multiple DbContexts in a C# project and I'm trying to enable migrations. When I specify the full command, i.e.:

Enable-Migrations -ContextTypeName Models.Account.AccountDetailDbContext

A migrations folder is created, with the configuration class, but I then get a message:

Checking if the context targets an existing database...

And then

The migrations configuration type 'Portal.WebUI.Migrations.Configuration' was not be found in the assembly 'Portal.WebUI'.

Even though it has just created the file, it can't find it.

I have the correct project selected in the Package Manager Console

I have tried the command using -verbose, but it gives no additional information

If I copy the dbcontexts and classes into a new project then it all works, so it must be something in this existing project that is making the migration fail, but I can't tell what it is.

3
  • Is your DbContext in Portal.WebUI project or in any other data access project?
    – Developer
    Commented Aug 17, 2016 at 16:15
  • 1
    Try adding -ContextProjectName Portal.WebUI to enable-migrations and make sure that project references the context project. Commented Aug 17, 2016 at 16:35
  • @SteveGreene That helped me. Although I had to use -ProjectName #ef6 Commented Jan 29, 2020 at 9:15

14 Answers 14

53

I solved this by adding EntityFrameworkCore\ before Add-Migration, i.e. the final statement was:

EntityFrameworkCore\Add-Migration

After giving a name for your new migration.

This "EntityFrameworkCore/" prefix is necessary to indicate that the migration should be added for a DbContext within the "EntityFrameworkCore" namespace or folder.

1
  • If I knew how to award you a bounty, I would give you 50 rep. You saved me a lot of trouble with the EntityFrameworkCore\ part. I was really close to deleting my project to start over after a ton of failed searches and out of sheer frustration. I had a lot of problems with IdentityDbContext that used to work but stopped after some unknown change I made a while ago.
    – Heki
    Commented Dec 23, 2022 at 16:42
20

I faced this problem. My solution:

  1. Exit visual studio

  2. Open your project again on visual studio

  3. Rebuild solution

Then the error removed. And I can run the command.

1
  • 1
    Just wanted to comment that as of 2022 this is still sometimes the correct solution.
    – Kirk Woll
    Commented Mar 26, 2022 at 0:05
8

After you run Enable-Migrations and the Configuration file is created, rebuild the project and run Enable-Migrations -Force again.

3

I had this problem and it was solved by changing the dropdown box at the top of the Package Manager Console to choose the correct project. You may need to maximise the width of the package manager console to see this box.

3

I managed to resolve this by uninstalling the EF nuget package and then reinstalling it.

0
3

I have stumbled upon this problem, and after endless hours of googling and trial and error, the solution to my specific problem was much easier.

Just make sure you have all your EF related nuget packages up to date.

I tried every answer I could find here, but what ended up working for me was: Select all EF related nuget packages (In my case)

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.Design
EntityFramework

And update them all, into their latest version (In my case) The issue was that one of the defaults I downloaded of one of these NuGet packages, was outdated and invalid.

1

Another possible issue worth checking: is your project signed? As I just discovered, this problem can also eventuate if the assembly is signed with a strong name key file. Part of the EntityFramework tool kit is migrate.exe which is called during the migration process. It appears if the assembly is signed, this application can't find the configuration type.

Solution seems to be <Project> → Properties → Signing: untick "Sign the assembly", at least while performing migration tasks. Tick it back when you're done.

0

I also had this issue because of a spelling mistake in a namespace

0

I was facing the same issue. What I found was that in my project name, "-" was included as "abc-xyz". I deleted my project and recreated it as "abcxyz" and it worked. Don't rename the project—you have to rename it at every reference. In this case, first uninstall EF and rename it, then install EF again.

0

I have faced this problem due to version update of EF package. I have solved by reinstalling:

1.Microsoft.EntityFrameworkCore.SqlServer

2.Microsoft.EntityFrameworkCore.Tools

3.Microsoft.EntityFrameworkCore.Design

4.Microsoft.EntityFrameworkCore

0

The solution is to check the project reference. In my case, I have added Entity Framework and Entity Framework Core references to my project.

To Resolve this I have removed Entity Framework Reference from the project and now EF Core migration commands are working.

0

Build your project and use 'EntityFramework\Add-Migration MigrationName instead of Add-Migration.

0

it was fixed for me by adding the package : Microsoft.EntityFrameworkCore.Design in the API and the Library Data

-1

In this case First, check NuGet packages

Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Design

most probably missing one of the above packages you get an error. if missing then install and

do migration using

Add-Migration InitialCreate
1
  • 1
    This has been answered a number of times now, please don't repeat existing answers. Commented Dec 20, 2022 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.