Jaime Santos
Jaime Santos

Reputation: 577

How to create a migration without "No migrations configuration type was found in the assembly" problem?

I am trying to create a migration in an ASP.NET Core Web Application, but when I try to do it, the package manager console returns a warning and an error:

Warning: "A version of Entity Framework older than 6.3 is also installed. The newer tools are running. Use 'EntityFramework\Add-Migration' for the older version."

Error: "No migrations configuration type was found in the assembly 'MyProjectName'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration)."

After doing some research I have found that a common problem is that the default project chosen in the package manager is not correct; but this is not my case, since it only gives me one option, and it is the project with which I am working. I have also found that a current solution is to enable the migrations with the "Enable-Migrations" command, but when I try to do this, it gives me the same warning and another error:

error: No context type was found in the assembly 'MyProjectName'.

I have also tried it the "enable-migraitons" command in a different way, "enable-migrations -contexttypename MyDBContextName", but this gives me another error:

error: The context type 'MyDBContextName' was not found in the assembly 'MyProjectName'.

But I actually have the following class:

    public class MyDBContextName: IdentityDbContext<UserModel>
    {
        public MyDBContextName(DbContextOptions<MyDBContextName> options) : base(options)
        { 
        }
    }

And I have this in my startup/configure services class:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDBContextName>(options => options.UseSqlServer("DefaultConnection"));

    services.AddIdentity<UserModel, IdentityRole>().AddEntityFrameworkStores<MyDBContextName>().AddDefaultTokenProviders();

    services.AddControllers();
}

Lastly, this is a picture of my NuGet packages:

Any Idea of what may be causing these errors? Thank you so much for your time, if you need more information I will provide it to you ass soon as I see your request. Have a good day. (:

Upvotes: 3

Views: 11444

Answers (6)

Jaime Santos
Jaime Santos

Reputation: 577

I still don't know what caused those errors, but after some changes I can now create migrations. Here are the 3 changes that I made:

  1. I installed Microsoft.EntityFrameworkCore.Tools

  2. I changed

services.AddDbContext<MyDBContextName>(options => options.UseSqlServer("DefaultConnection"));

for

services.AddDbContext<MyDBContextName>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

  1. Finally, in my app.setting.json I changed
"connectionStrings": 
{
    "DefaultConnection": "Data Source=MyName;Initial Catalog=PracticaApiSecurity;Integrated Security=True"
},

for

  "connectionStrings": {
    "DefaultConnections": "Data Source=MyName;Initial Catalog=PracticaApiSecurity;Integrated Security=True"
  },

changed connection for connectionS

Upvotes: 3

r2d2
r2d2

Reputation: 637

Selecting the right project in the packet manager console solved the problem.

screenshot

Upvotes: 1

Rich Dingle
Rich Dingle

Reputation: 11

Running: PM> EntityFrameworkCore\Add-Migration

Worked for me:

enter image description here

Upvotes: 1

LRitter
LRitter

Reputation: 154

I got rid of this error by closing my instance of Visual Studio 2019 and reopening it.

Upvotes: 1

Anthony Griggs
Anthony Griggs

Reputation: 1641

In my case I had everything configured and setup correctly so none of these suggestions worked. I took a bit more research to discover that my issue was that I also had the EntityFrameWork package installed along with EntityFrameWorkCore. I was using the later for some older functions I had in my Library which executed SqlCommand.

Installing both packages completely scrubbed my project so I ended up having to completely reset my EntityFramework Migrations by following this suggestion: Reset Entity Framework

Upvotes: 0

Parth Patil
Parth Patil

Reputation: 21

Installing installed Microsoft.EntityFrameworkCore.Tools worked for me

Upvotes: 0

Related Questions