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.
dotnet ef -s ../{asp.net core app dir} migrations add FirstMigration
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
@bricelam in triage we said this was a duplicate of an other issue, but I am not sure of which one. Can you help?
@armartinez If you're interesting it trying out the latest tools where a lot of issues like this have been fixed, see #7358
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 Using --force should avoid the error.
I had to use --startup-project and point to a console app
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 - 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?
cd gis.database/
dotnet ef migrations add MyMigration --startup-project ../gis.web/
Thanks for that Brice.
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
@tomchovanec thanks for that, but I'm an old Unix hack, so click is perfect for me :-)
Click=CLI stupid autocorrect :-)
Just wondering if the requirement for an executable project will ever be removed it is so annoying.
@aday12345 It comes down to priorities. It's a lot of work, and there are probably more valuable things things we could do instead.
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.
Most helpful comment