EF Core Migrations in Separate Project – Issues

ef-core-2.0entity-framework-migrations

I am not successful in adding ef core migrations in a separate project. I have provided the project structure at the end.

The DbContext class and the migrations are in separate projects such that the former is a class library and the migrations project is a console application. I added a reference to the DbContext project in the migrations project however, I am getting errors while running migration commands

\EF.BlogsDb.Migrations > dotnet ef migrations add InitialCreate –project EF.BlogsDb.Migrations.csproj

No DbContext was found in assembly 'EF.BlogsDb.Migrations'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

I followed advise from this link but I am not sure of the following.

In which project should i add these lines? is it the migrations project or my web project or the DbContext project?

options.UseSqlServer(
    connectionString,
    x => x.MigrationsAssembly("MyApp.Migrations")); 

Why should i Add a reference to the migrations assembly from the startup assembly?

From which location/folder/project should i run this command?

dotnet ef migrations add NewMigration --project MyApp.Migrations 

I can share the source code if you can tell me how to do that.

Thanks in advance for the clarifications one might provide!

Here is my project structure looks like:

Project Structure

Best Answer

The issue is solved using the following command.

dotnet ef --startup-project ../Project.Api/ migrations add InitialModel
  • Here I am using separate folder for the api and the EF.
  • Run the above command from the EF project folder
  • Start up project means referred to the api
Related Topic