efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Tools: Support .NET Core Class Library projects

Open natemcmaster opened this issue 10 years ago • 43 comments

Add support for running "dotnet ef" on a .NET Standard and desktop .NET class library project.

This is blocked by https://github.com/dotnet/cli/issues/2645 ~~and/or https://github.com/dotnet/cli/issues/4005 (depending on our implementation approach)~~.

Short explanation

If you cannot do dotnet run, you also cannot use dotnet ef on your project.

More explanation

.NET Core, unlike .NET Framework, does not have a globally installed framework. All .NET Core apps need to reference an app framework (e.g. Microsoft.NETCore.App) in order to run. .NET Core class library projects are built without a framework, so dotnet-ef cannot run on these projects.

Workarounds

See https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html#targeting-class-library-projects-is-not-supported for workarounds.

natemcmaster avatar May 10 '16 18:05 natemcmaster

Hi,

Just to be sure, we can't use EF within a class library project (aka tests project) in RC2?

Kurira avatar May 19 '16 17:05 Kurira

@Kurira you can use EF within a class library. You can't use EF .NET Core CLI commands on a class library. See https://docs.efproject.net/en/latest/cli/dotnet.html more info

natemcmaster avatar May 19 '16 17:05 natemcmaster

So I would use tools from my web project and apply them to test context which resides in test project? Your link talks about transforming it into an app, but I always get System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, even then used from web project template.

Kurira avatar May 19 '16 18:05 Kurira

I need to test this more, but here is my solution that seems to be working: https://github.com/aspnet/EntityFramework/issues/5460#issuecomment-221167712

kjbetz avatar May 24 '16 12:05 kjbetz

Is there a published workaround for this issue? I've added:

"frameworks": {
    "net451": {
        "buildOptions": {
            "emitEntryPoint": true
        }
    },
    "netcoreapp1.0": {
        "imports": [ "portable-net451+win8" ],
        "buildOptions": {
            "emitEntryPoint": true
        },
        "dependencies": {
            "Microsoft.NETCore.App": {
                "type": "platform",
                "version": "1.0.0-*"
            }
        }
    }
},

and a main() in program.cs, but I still get the "..Entity Framework tools does not support targeting class library projects.." message.

ToddThomson avatar May 25 '16 22:05 ToddThomson

@ToddThomson you are probably hitting https://github.com/dotnet/cli/issues/3164. Try setting the top-level emitEntryPoint build option to true.

natemcmaster avatar May 25 '16 22:05 natemcmaster

@natemcmaster Yes. That was it.

ToddThomson avatar May 25 '16 23:05 ToddThomson

When we have this implemented we need to verify that the right-click->Publish migrations feature works when the context/migrations is in a class library - I suspect it is not going to work by default because it will likely need to specify some of the target/startup project parameters.

rowanmiller avatar May 26 '16 17:05 rowanmiller

Additionally, if your DbContext takes a parameter then you also need to add a simple startup.cs to the class library. I added:

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices( IServiceCollection services )
    {
        // Add Identity framework services.
        services.AddDbContext<SecurityDbContext>( options =>
             options.UseSqlServer( Configuration.GetConnectionString( "DefaultConnection" ) ) );

    }

This was sufficient for me to add an initial migration for Identity services.

ToddThomson avatar May 26 '16 17:05 ToddThomson

@natemcmaster Any change in this regard in latest tooling preview 2?

HamidMosalla avatar Jun 27 '16 18:06 HamidMosalla

We are still blocked on .NET Core CLI support for this. https://github.com/dotnet/cli/issues/2645.

Preview 2 added --startup-project which may mitigate the limitation for some project setups (https://github.com/aspnet/EntityFramework/pull/5680).

natemcmaster avatar Jun 27 '16 19:06 natemcmaster

Hi, where can i use this "--startup-project" to fix that? I'm trying install EntityFrameworkCore.Tools on class library and get "Package Microsoft.EntityFrameworkCore.Tools 1.0.0-preview2-final is not compatible with netstandard1.6" 10x!

arielcloud avatar Jun 28 '16 08:06 arielcloud

@arielcloud did you upgrade your version of .NET Core CLI to preview 2?

natemcmaster avatar Jun 28 '16 15:06 natemcmaster

@natemcmaster i dont sure I have CLI at all.

I downloaded .net core by "Visual Studio users: The best way to develop with .NET Core on Windows is using Visual Studio. You will need Visual Studio 2015 Update 3 and then download the .NET Core for Visual Studio official MSI Installer" https://www.microsoft.com/net/core#windows

where can i see if i have CLI and it's version? 10x!

arielcloud avatar Jun 28 '16 17:06 arielcloud

@arielcloud execute dotnet.exe --version from the command line. Also, Microsoft.EntityFrameworkCore.Tools only supports "netcoreapp1.0". It cannot be installed into a class library. https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html#targeting-class-library-projects-is-not-supported

natemcmaster avatar Jun 29 '16 16:06 natemcmaster

I've had lots of questions about this, so I've updated the original issue description above ⏫ to provide a little more explanation about why this feature is blocked and how you can workaround it.

FWIW this is feature is not simple to fix. Class libraries are not "runnable" by design (i.e. that's what it means to be a library, not an app). dotnet-ef needs to run the code to inspect the model and migrations, which is why it requires the target project be "runnable". See https://github.com/dotnet/cli/issues/2645#issuecomment-214591028

natemcmaster avatar Jun 29 '16 16:06 natemcmaster

I've written up how I worked around the issue for anyone who is curious. It's based on the link given in the OP, but with a couple of tweaks based on experience from my real life project. http://benjii.me/2016/05/dotnet-ef-migrations-for-asp-net-core/

bjcull avatar Jun 29 '16 19:06 bjcull

@natemcmaster my version is 1.0.0-preview2-003121. @bjcull please add create date / update date to your great blogs :) i'll try use those workarounds. 10x!

arielcloud avatar Jun 29 '16 21:06 arielcloud

Works great, except that Resharper mark most of classLibrary-EF code in red... i noted them

arielcloud avatar Jun 29 '16 23:06 arielcloud

Is there a problem with the way I went about doing this? https://github.com/aspnet/EntityFramework/issues/5460#issuecomment-221167712

Seems to work pretty well. Migrations are in my Api project... but everything else is in the class library and class library doesn't have to be turned into an app.

kjbetz avatar Jun 30 '16 15:06 kjbetz