Generally we keep DbContext class in Web api or MVC project where Startup class is present. Recently, I have created a project based on clean architecture. In this project, my DbContext class is in another project name Ordering.Infrastructure rather than Ordering.API. My Project structure as like –
- Ecommerce - Ordering.API - Ordering.Application - Ordering.Core - Ordering.Infrastructure
In this application, Startup class is in Ordering.API project which is an API project and DbContext class is in Ordering.Infrastructure class as mentioned earlier. I run the following script in Ordering.API project and got the following error.
PM> Add-Migration initial Build started... Build succeeded. Your target project 'Ordering.API' doesn't match your migrations assembly 'Ordering.Infrastructure'. Either change your target project or change your migrations assembly. Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("Ordering.API")). By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.
Step 1: Install the following nuget packages in Ordering.Infrastructure where DbContext class is present.
Install-Package Microsoft.EntityFrameworkCore Install-Package Microsoft.EntityFrameworkCore.Design Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.Tools
Step 2: Run the migration command in Ordering.Infrastructure project. Before running the following command, make sure your Ordering.Infrastructe is selected as your default project.
PM> Add-Migration initial PM> Update-Database -Verbose