Bercovici Adrian
Bercovici Adrian

Reputation: 9370

How to create migration when DBContext in different assembly

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

*root
  -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

 *root
      -App
      -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.

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

<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>

Upvotes: 3

Views: 7136

Answers (4)

Chathuranga Liyanage
Chathuranga Liyanage

Reputation: 13

I used the full Directory path and it's worked. And also we should use correct project references.

ex: dotnet ef migrations add Initial --project D:\NK-Pro\API\TaskAPI.DataAccess\TaskAPI.DataAccess.csproj

Upvotes: 0

Ted Mosby
Ted Mosby

Reputation: 77

First change directory to project directory, then add .csproj

$ dotnet ef migrations add InitialCreate --project DataAccess.csproj

I hope it works!

Upvotes: 1

Dvd Franco
Dvd Franco

Reputation: 656

the answer given by @Ajay above finally worked for me, after days and days googling for it:

to insert the migrations assembly complete path in the --project argument, like this:

dotnet ef migrations add Mymigration --project "c:\repos\myproject\Data" 

...and you should execute this command from your main startup project (like c:\repos\myproject\Web)

Upvotes: 7

wanibani
wanibani

Reputation: 51

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!

Upvotes: 5

Related Questions