Tools: Support .NET Core Class Library projects
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.
Hi,
Just to be sure, we can't use EF within a class library project (aka tests project) in RC2?
@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
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.
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
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 you are probably hitting https://github.com/dotnet/cli/issues/3164. Try setting the top-level emitEntryPoint build option to true.
@natemcmaster Yes. That was it.
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.
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.
@natemcmaster Any change in this regard in latest tooling preview 2?
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).
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 did you upgrade your version of .NET Core CLI to preview 2?
@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 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
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
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/
@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!
Works great, except that Resharper mark most of classLibrary-EF code in red... i noted them
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.