Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
armartinez opened this issue Jan 3, 2017 · 16 comments
Closed

Comments

@armartinez
Copy link

armartinez commented Jan 3, 2017

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 armartinez changed the title Can´t generate migrations using ..NET Core Tools MSBuild Can´t generate migrations using .NET Core Tools MSBuild from the CLI Jan 3, 2017
@divega
Copy link
Contributor

divega commented Jan 17, 2017

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

@bricelam
Copy link
Contributor

#5320

@bricelam
Copy link
Contributor

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

@CumpsD
Copy link

CumpsD commented Mar 13, 2017

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'

@bricelam
Copy link
Contributor

@CumpsD Using --force should avoid the error.

@CumpsD
Copy link

CumpsD commented Mar 13, 2017

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

@bricelam
Copy link
Contributor

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.

@shawty
Copy link

shawty commented Aug 18, 2017

@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?

@bricelam
Copy link
Contributor

bricelam commented Aug 18, 2017

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

@shawty
Copy link

shawty commented Aug 18, 2017

Thanks for that Brice.

@tomchovanec
Copy link

tomchovanec commented Oct 3, 2017

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.

@shawty
Copy link

shawty commented Oct 5, 2017

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

@shawty
Copy link

shawty commented Oct 5, 2017

Click=CLI stupid autocorrect :-)

@adayIvey
Copy link

adayIvey commented Apr 6, 2018

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

@bricelam
Copy link
Contributor

bricelam commented Apr 6, 2018

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

@chaim1221
Copy link

chaim1221 commented May 11, 2019

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.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants