3

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>
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • 1
    In same project structure below command works for me. CLI opens in App folder App> dotnet ef migrations add your_migrationName --project "Absolute path to your migration assembly (c:/root/migrations)" – Ajay Jul 17 '20 at 10:38
  • Answer is here. https://stackoverflow.com/questions/38705694/add-migration-with-different-assembly – Ajay Jul 17 '20 at 10:46
  • Does this answer your question? [Add migration with different assembly](https://stackoverflow.com/questions/38705694/add-migration-with-different-assembly) – Gert Arnold Nov 13 '22 at 09:08

4 Answers4

7

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)

Dvd Franco
  • 636
  • 7
  • 6
5

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!

wanibani
  • 51
  • 2
1

First change directory to project directory, then add .csproj

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

I hope it works!

Ted Mosby
  • 67
  • 1
  • 5
0

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