PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 13, 2022

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

 April 13, 2022     asp.net-core, dbcontext, entity-framework, migration, visual-studio     No comments   

Issue

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


Solution

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



Answered By - Jaime Santos
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing