efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Can´t generate migrations using .NET Core Tools MSBuild from the CLI

Open armartinez opened this issue 9 years ago • 16 comments

I can´t generate a new migration from a class library project even when setting a valid ASP.NET Core Web API application as a startup project.

Error message: Could not invoke this command on the startup project '{project-dir}'. This version of the Entity Framework Core .NET Command Line Tools does not support commands on class library projects in ASP.NET Core and .NET Core applications. See http://go.microsoft.com/fwlink/?LinkId=798221 for details and workarounds.

Steps to reproduce

  • Execute the following command on the library project folder :
dotnet ef -s ../{asp.net core app dir} migrations add FirstMigration

Further technical details

EF Core version: 1.1.0 EF Core Tools version: 1.0.0-msbuild2-final Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Mac OS 10.12.2 IDE: Visual Studio Code

armartinez avatar Jan 03 '17 14:01 armartinez

@bricelam in triage we said this was a duplicate of an other issue, but I am not sure of which one. Can you help?

divega avatar Jan 17 '17 21:01 divega

#5320

bricelam avatar Jan 20 '17 18:01 bricelam

@armartinez If you're interesting it trying out the latest tools where a lot of issues like this have been fixed, see #7358

bricelam avatar Jan 20 '17 18:01 bricelam

It seems to work with netstandard libraries in 2017, except dotnet ef migrations remove which still throws:

Startup project 'Wegwijs.SqlServer.csproj' targets framework '.NETStandard'. This framework is not intended for execution and may fail to resolve runtime dependencies. If so, specify a different project using the --startup-project option and try again.
System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

CumpsD avatar Mar 13 '17 05:03 CumpsD

@CumpsD Using --force should avoid the error.

bricelam avatar Mar 13 '17 15:03 bricelam

I had to use --startup-project and point to a console app

CumpsD avatar Mar 13 '17 15:03 CumpsD

Oh that's right. SQL Server is packaged in a way that won't work with just .NET Standard. With SQLite, any command that doesn't open a database connection should work.

bricelam avatar Mar 13 '17 15:03 bricelam

@bricelam - Hi Brice, can you give us an example please?

Iv'e just hit this in a dotnet core web app using npgsql, and I'm not exactly clear on how to proceed.

Actual Exception is:

Startup project 'gis.database.csproj' targets framework '.NETStandard'. This framework is not intended for execution and may fail to resolve runtime dependencie s. If so, specify a different project using the --startup-project option and try again. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.EntityFrameworkCore.Design, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. File name: 'Microsoft.EntityFrameworkCore.Design, Culture=neutral, PublicKeyToken=null' at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMar k& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.Assembly.Load(AssemblyName assemblyRef) at Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor..ctor(String assembly, String startupAssembly, String projectDir, String contentRootPath, String dataDirectory, String rootNamespace, String environment) at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor() at Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute() at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args) at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args) Could not load file or assembly 'Microsoft.EntityFrameworkCore.Design, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

My EF stuff is in a seperate .NETStandard class library, in a folder called "gis.database", while the actual web app running standalone is in '../gis.web/'

How can I make ef migrations run in this case, based on your comments above?

shawty avatar Aug 18 '17 11:08 shawty

cd gis.database/
dotnet ef migrations add MyMigration --startup-project ../gis.web/

bricelam avatar Aug 18 '17 15:08 bricelam

Thanks for that Brice.

shawty avatar Aug 18 '17 16:08 shawty

There's an easier way. Make your DB project executable (with dummy Main method).

<PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <OutputType>Exe</OutputType>
   ....
  </PropertyGroup>

And implement IDesignTimeDbContextFactory class.

tomchovanec avatar Oct 03 '17 13:10 tomchovanec

@tomchovanec thanks for that, but I'm an old Unix hack, so click is perfect for me :-)

shawty avatar Oct 05 '17 22:10 shawty

Click=CLI stupid autocorrect :-)

shawty avatar Oct 05 '17 22:10 shawty

Just wondering if the requirement for an executable project will ever be removed it is so annoying.

adayIvey avatar Apr 06 '18 17:04 adayIvey

@aday12345 It comes down to priorities. It's a lot of work, and there are probably more valuable things things we could do instead.

bricelam avatar Apr 06 '18 20:04 bricelam

Trying the method above I just get

Unable to create an object of type 'AppContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

... --verbose tells me that it looks for CreateWebHostBuilder (we have a custom implementation, but the CreateOrMigrateDatabase method is there), looks for IDesignTimeFactory implementations (which I am trying to avoid)... looks for (and misses!) the application service provider (where I've defined AddDbContext with the provider!), and looks for "IWebHost accessor"... which... exists, just not in the format that it wants.

The reason I wanted to avoid IDesignTimeFactory is because the information in there is already in the DI pipeline....

EDIT:

"just not in the format that it wants." Exactly.

https://stackoverflow.com/a/54222348/2496266

You must call it CreateWebHostBuilder ...no other custom name will work.

chaim1221 avatar May 11 '19 10:05 chaim1221