Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net core + EF Code first, migration files in different project

In my solution I want to use Asp.net core + EF Code first

I have 2 projects:

  • CC.API
  • CC.Infrastructure

In CC.API I have startup class and there is:

services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("CC.Infrastructure")));

(Connection string is in appsettings.json)

As you can see I'm trying to keep migration files in different project - CC.Infrastructure.

Unfortunately whilst Add-Migration Init I receives an error: Your target project 'PK.API' doesn't match your migrations assembly 'PK.Infrastructure'. Either change your target project or change your migrations assembly

If I will change in startup b => b.MigrationsAssembly("CC.API") then everthing works fine, but files migration files will be in CC.API :/

like image 312
DiPix Avatar asked Jun 19 '17 18:06

DiPix


1 Answers

This is/was a longstanding issue with EF Core. The solution used to be making your class library an executable (temporarily) and then run all EF operations against it.

With current tooling, you can just run Add-Migration while in the library folder; the only caveat is you need to set the startup-project flag to the actual executable's project.

So the command ends up being something like:

C:\CC.Infrastructure>dotnet ef migrations add NewMigration --startup-project ../CC.API/CC.API.csproj
like image 87
BradleyDotNET Avatar answered Sep 28 '22 07:09

BradleyDotNET