9
votes

I have an ASP.NET Core Web Application (.NET Framework) project targeting .NET Framework 4.6.2.

I have introduced Entity Framework into the web project and have set up and run

dotnet ef migrations add Initial

from the web project directory. The migration is created, all is good.

I want to refactor the code so that my entities and db context are in a class library instead. So I have created a separate Class Library (.NET Framework) project, also targeting .NET Framework 4.6.2.

Still from the web project directory I run

dotnet ef migrations add Initial --startup-project . --project ..\MigrationTest.Entities

where MigrationTest.Entities is my class library.

This time I get:

C:\Projects\Experiments\MigrationTestNoIdentity\MigrationTest.Entities\obj\MigrationTest.Entities.csproj.EntityFrameworkCore.targets(4,5): error MSB4006: There is a circular dependency in the target dependency graph involving target "GetEFProjectMetadata". [C:\Projects\Experiments\MigrationTestNoIdentity\MigrationTest.Entities\MigrationTest.Entities.csproj] 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.

It isn't a .NET Core project, so I try creating a .NET Core project instead. Running the migrations command then gives:

C:\Program Files\dotnet\sdk\1.0.3\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.Common.targets(73,5): error : Project 'C:\Projects\Experiments\MigrationTestNoIdentity\MigrationTest.Entities.Core\MigrationTest.Entities.Core.csproj' targets '.NETCoreApp,Version=v1.1'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.6.2'. [C:\Projects\Experiments\MigrationTestNoIdentity\MigrationTest.Entities.Core\MigrationTest.Entities.Core.csproj] C:\Program Files\dotnet\sdk\1.0.3\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.targets(92,5): error : Cannot find project info for 'C:\Projects\Experiments\MigrationTestNoIdentity\MigrationTest.Entities.Core\MigrationTest.Entities.Core.csproj'. This can indicate a missing project reference. [C:\Projects\Experiments\MigrationTestNoIdentity\MigrationTest.Web\MigrationTest.Web.csproj]

And of course, the Core project cannot be referenced by the .NET project.

Is what I am trying to do a supported scenario? Is there some problem with my command line usage? Something else?

2
If the core web project references a core class library, then all is well. Perhaps this is a limitation on .net class libraries.fractor
Also successful with .NET Framework web app referencing a .NET Standard class library.fractor
I guess the dotnet ef tooling doesn't support targeting a .NET Framework class library even if the referencing project is an ASP.NET Core (.NET Framework) project, despite migrations working for the web project itself. This is probably reasonable and beyond the scope of the .NET Core tooling, although a more helpful message would be good.fractor
I am also having same problem. Were you able to figure out any solution for this?rout0802
@rout0802 See comment above.fractor

2 Answers

5
votes

It's worth checking to ensure you have the EF Tools package installed.

Open the Package Manager console in Visual Studio and execute:

Install-Package Microsoft.EntityFrameworkCore.Tools

Then try your dotnet ef <commands> again.

1
votes

Super stupid, but I had to use a forward slash because I was on Mac.

So in the OP's example:

dotnet ef migrations add Initial --startup-project . --project ../MigrationTest.Entities

I was running the command from the project that contains the DatabaseContext, then the command is as follows:

dotnet ef migrations add Initial --startup-project "../MyProject.Api"