You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tools: Support .NET Core Class Library projects #5320
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.
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.
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.
To ellaborate, there are two projects when you invoke a command:
The target project that you are operating on. Files will be added to this project.
The startup project. This is the runtime environment we'll emulate at design time. This project's config will be loaded; it's Startup.ConfigureServices() will be used if it's an ASP.NET Core project; etc.
In PMC, the target project defaults to the selected Default project and can be overridden using -Project. The startup project defaults the the selected startup project (the one that runs when you click Start with debugging) and can be overridden using -StartupProject.
On CLI, the target project defaults to the one in the working directory and can be overridden using --project. The startup project defaults to the target project and can be overridden using --startup-project.
Bad news The solution I had stopped working. You will have to use a separate startup project when working directly with .NET Standard projects. You will receive the following error.
Startup project 'ClassLibrary1' targets framework '.NETStandard'. This framework is not intended for execution. Specify a project that targets a concrete framework like '.NETCoreApp' or '.NETFramework' using the --startup-project option and try again.
Good news
They're adding a .NET Core (in addition to the .NET Standard) Class Library in Visual Studio 2017. The Entity Framework Core Tools will work as expected on these projects.
Can we add a way to persist the startup project in our class library so we don't need to pass the argument on every call and also we can share which startup project should be used in source control ? (Thinking in multiple project sharing (or not) the same dbcontext.
maybe dotnet ef --set-startup-project ..\MyApp, a key on csproj like user secrets or just a StartupProject.txt file :)
Activity
Kurira commentedon May 19, 2016
Hi,
Just to be sure, we can't use EF within a class library project (aka tests project) in RC2?
natemcmaster commentedon May 19, 2016
@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
Kurira commentedon May 19, 2016
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.kjbetz commentedon May 24, 2016
I need to test this more, but here is my solution that seems to be working: #5460 (comment)
ToddThomson commentedon May 25, 2016
Is there a published workaround for this issue? I've added:
and a main() in program.cs, but I still get the "..Entity Framework tools does not support targeting class library projects.." message.
natemcmaster commentedon May 25, 2016
@ToddThomson you are probably hitting https://github.com/dotnet/cli/issues/3164. Try setting the top-level emitEntryPoint build option to true.
ToddThomson commentedon May 25, 2016
@natemcmaster Yes. That was it.
rowanmiller commentedon May 26, 2016
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.
ToddThomson commentedon May 26, 2016
Additionally, if your DbContext takes a parameter then you also need to add a simple startup.cs to the class library. I added:
This was sufficient for me to add an initial migration for Identity services.
49 remaining items
jmevel commentedon Nov 30, 2016
@bricelam so you are saying there will be no issues with a .NET Core class library targeting only full .NET Framework instead of .NET Standard?
bricelam commentedon Nov 30, 2016
Correct. Class libraries targeting .NET Framework or .NET Core App should work just fine without specifying a separate startup project.
bricelam commentedon Nov 30, 2016
To ellaborate, there are two projects when you invoke a command:
The target project that you are operating on. Files will be added to this project.
The startup project. This is the runtime environment we'll emulate at design time. This project's config will be loaded; it's
Startup.ConfigureServices()will be used if it's an ASP.NET Core project; etc.In PMC, the target project defaults to the selected Default project and can be overridden using
-Project. The startup project defaults the the selected startup project (the one that runs when you click Start with debugging) and can be overridden using-StartupProject.On CLI, the target project defaults to the one in the working directory and can be overridden using
--project. The startup project defaults to the target project and can be overridden using--startup-project.ErikEJ commentedon Nov 30, 2016
For the first time I kind of understand this, how about adding this to the docs?
rowanmiller commentedon Nov 30, 2016
@ErikEJ good idea. We don't really have any detailed docs on the commands at the moment, just a reference on what parameters are available etc.
bricelam commentedon Jan 4, 2017
Bad newsThe solution I had stopped working. You will have to use a separate startup project when working directly with .NET Standard projects. You will receive the following error.Good news
They're adding a .NET Core (in addition to the .NET Standard) Class Library in Visual Studio 2017. The Entity Framework Core Tools will work as expected on these projects.
bricelam commentedon Jan 5, 2017
Scratch that. It was an unrelated issue. .NET Standard class libraries will "work" (as much as they can)
Bartmax commentedon May 28, 2017
Can we add a way to persist the startup project in our class library so we don't need to pass the argument on every call and also we can share which startup project should be used in source control ? (Thinking in multiple project sharing (or not) the same dbcontext.
maybe
dotnet ef --set-startup-project ..\MyApp, a key on csproj like user secrets or just aStartupProject.txtfile :)Bartmax commentedon May 29, 2017
discard my last comment. I found a better way that I think it should be the official way because it's easy and afaik there's no downside.
To make your class library be able to run
dotnet efit's as simple as targeting both frameworks.Example:
this is also mentioned on the "workarounds"
qdxiayongming commentedon Sep 25, 2017
6 hours.... worked....
中文:
1)修改项目文件,添加项目Nuget包引用以及Cli引用:EFCore.Tools&EFCore.Tools.Design.
2)在PMC中(程序包控制台)通过cd命令将目录转移到对应的项目目录下(解决方案默认打开的都是比项目更高一级的解决方案目录,此时应该通过CD转换到项目目录下。
3)使用 dotnet ef命令,就可以看到独角兽了。