Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net-core entity-framework migration visual-studio dbcontext

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

发布于 2020-04-10 10:21:57

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. (:

Questioner
Jaime Santos
Viewed
432
Jaime Santos 2020-02-02 02:13

I still don't know what caused those errors, but after some changes now I can 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")));

3)Finally, in my app.setting.json I changed

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

for

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

changed connection for connectionS