How to create migration when DBContext in different assembly

ef-migrations entity-framework-core msbuild


I have NET Core 3.1 application where my [models,dbcontext,application] are all in their own assemblies.I am trying to issue a migration(s) for the application.I keep getting this error:

$ dotnet ef migrations add InitialCreate --project DataAccess 

MSBUILD : error MSB1009: Project file does not exist. Switch: DataAccess Unable to retrieve project metadata. Ensure it's an MSBuild-based .NET Core project. If you're using custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.

Current Structure

  -App  (.NET Core 3.1)
  -DataAccess  (.NET Standard 2.1)   (Contains the DBContext)
  -Models     (.NET Standard 2.1)   (contains models)

I have also tried creating a separate assembly for migrations and use the MigrationAssembly extension in my App :

services.AddDbContext<[some DBContext]>(x => x.UseSqlServer([some string],x=>x.MigrationsAssembly("Migrations")));

Tried Structure

      -Migrations (.NET Standard 2.1)
      -DataAccess (.NET Standard 2.1)
      -Models (.NET Standard 2.1)

I do not understand how this should be done.I want to be able to do migrations and ideally i would like to keep them in their own assembly.Currently i can't do them at all.

I have also tried adding this to my App csproj file:

3/9/2020 10:08:26 AM

Popular Answer

exactly same issue here, since hours, good to know not being the only one getting crazy about!

Finally worked it out: I'm using EFCore.Tools in Package Manager Console instead of dotnet Cli:

//Add Migration

Add-Migration *name* -Project *Library project* -StartupProject *StartUp/Web project*

//Update Database

Update-Database -Project *Library project* -StartupProject *StartUp/Web project* -verbose

i would go this direction... have fun!

3/13/2020 9:27:36 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow