I have created a new .NET Core project with dotnet new -t lib command. It's a class library for my EF Context. This is my project.json:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable"
},
"dependencies": {},
"frameworks": {
"netstandard1.6": {
"dependencies": {
"NETStandard.Library": "1.6.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
}
}
}
When I run dotnet restore command I'm getting this error:
error: Package Microsoft.EntityFrameworkCore.Tools 1.0.0-preview2-final is not compatible with netstandard1.6 (.NETStandard,Version=v1.6). Package Microsoft.EntityFrameworkCore.Tools 1.0.0-preview2-final supports:
How I can use EF Code First with netstandard1.6?
According to this, The "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final" should be registered in a tools section in project.json:
{
"version": "1.0.0-*",
"description": "Class Library",
"frameworks": {
"netstandard1.6": {
"imports": [
"dnxcore50"
]
}
},
"dependencies": {
"NETStandard.Library": "1.6.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0"
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
}
Also it is a good idea to use imports, because some of the EF Core’s dependencies still don't support .NETStandard, but in your case I checked and it works even without imports.
Regarding the Johan B's comment, which states ef tools cannot be used with class libraries for now, it is partly addressed by using --startup-project workaround, for example: dotnet ef --startup-project ../ProjectThatCanRunCommand/ migrations add InitDb, more info: Parameterize directories used by dotnet-ef.
Related
as a beginner I tried to follow right way to create a class library project or ASP.Net core webApi. The situation of mine is:
1) I created a class library project (let's call it 'AAA') using .net Core. The framework for a class library should be "netstandard1.6" I know. I defined 1 static class and its methods.
2) Then I create another Asp.Net Core WebApi project, and let's call it 'BBB' The default framework in project.json file is "netcoreapp1.0" then.
Then in project BBB -- 'References' I add project AAA as reference. No error happens. However if I try to use the static class/method defined in prject AAA, Visual studio told me that static class could not be found. (Instead of traditional .Net project it will auto-pop suggestion and if I click the link, VS automatically add code and sort it out for me.)
But if I move mouse on top of the red error part, VS (or re-sharper) did suggest like "Reference prject AAA... NETStandard, Version=v1.6' and use project AAA.static class/method....". However even if I click on the suggestion, there is still NO changes at all.
How come for that and how could I fix it?
Thanks a lot!!
The .NET Standard library framework is used by the .NET Core class library, but not by your .NET Core application.
So your class library has a project.json like this :
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
And to reference this class library into a .NET Core application, you have to add the depencency, with the default imports and it will work :
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"ClassLibrary1": "1.0.0-*"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
With this both project.json files, the reference is great and if I add a static class in my class library like :
public static class Class2
{
public static void Test()
{
}
}
I can call this method in a web app controller, like :
[HttpGet]
public void TestStaticCall()
{
ClassLibrary1.Class2.Test();
}
I finally solved this from this post: https://github.com/aspnet/Tooling/issues/664. Update Resharper did help! Thanks a lot every one!!
On VSTS, I'm trying to define a build definition for my ASP.NET Core app targeting .NET Framework 4.6.2
I see these two templates on VSTS Build but not sure what the difference between the two is and which one I should use.
I tried both and got different errors. Which build template is a good start for me? My app currently is a very simple "Hello World" app but it does have a couple of class library projects in it. I did that to simulate what my app will look like when I'm done.
So, my solution looks like this:
-- ASP.NET Core app (Targeting .NET 4.6.2)
-- Class Library 1 (Targeting .NET 4.6.2)
-- Class Library 2 (Targeting .NET 4.6.2)
Both class libraries are the classic type -- see below
UPDATE:
I tried following this article which is really for an ASP.NET solution built on .Net Core.
https://www.visualstudio.com/en-gb/docs/build/apps/aspnet/aspnetcore-to-azure
However, I'm getting the error in Step 2: dotnet restore:
Errors in C:\a\1\s\Dev\src\MyProject\project.json
2016-11-07T17:17:33.3792225Z Unable to resolve 'ClassLibrary1' for '.NETFramework,Version=v4.6.2'.
2016-11-07T17:17:33.3792225Z Unable to resolve 'ClassLibrary2' for '.NETFramework,Version=v4.6.2'
What modifications do I need to make so that I can apply these steps to my project?
UPDATE 2:
Here's my project.json file
{
"dependencies": {
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
},
"tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"net462": {
"dependencies": {
"ClassLibrary1": {
"target": "project"
},
"ClassLibrary2": {
"target": "project"
}
}
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},
"scripts": {
"prepublish": [ ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
UPDATE 3:
This is the current set of tasks in the build definition. This is directly from ASP.NET Build (PREVIEW) except I removed the test step and changed the NuGet version to 3.5.0 - build 1938 (rc2). Also, the MSBuild arguments are follows:
/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"
Dotnet restore command can't restore local project dependency. More information, you can refer to this thread.
My steps:
Create a build definition with ASP.NET Build (PREVIEW) template
Change nugget version to 3.5.0
Note: The Hosted Build Agent includes .Net Core 1.0 with Preview 2 Tooling, so you need to use corresponding version of SDK.
global.json:
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-preview2-003121"
}
}
I've tried to get NancyFx running with .Net Core under Ubuntu and Windows but I get the message that NancyFx it not compatible with .NetCoreApp.
I'm new to the whole .Net Core thing, so any help is welcome.
My project.json
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": "1.0.1",
"Nancy": "2.0.0-barneyrubble"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Error Message on dotnet restore
Errors in D:\User\Documents\Visual Studio 2015\Projects\WebService\src\WebService\project.json
Package Nancy 2.0.0-barneyrubble is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Nancy 2.0.
0-barneyrubble supports:
- net452 (.NETFramework,Version=v4.5.2)
- netstandard1.6 (.NETStandard,Version=v1.6)
One or more packages are incompatible with .NETCoreApp,Version=v1.0.
dotnet --version
1.0.0-preview2-003131
There are a few tutorial around which show people using Nancy on .Net Core, so am I doing something wrong?
I've found the answer. The problem was the missing type in the Microsoft.NETCore.App dependency.
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Nancy": "2.0.0-barneyrubble"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Builds perfectly on Windows and Linux.
I'm using ASP.NET Core 1.0 with preview 2 tooling.
I moved the netcoreapp1.0 section from dependencies to netcoreapp1.0 in frameworks like so:
"frameworks": {
"netcoreapp1.0": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
}
}
}
Now when I deploy the app, it complains about there is no runtimes section specifying an RID. And when I add that it is deployed as a self-contained app.
Is this a bug?
The project.json should look like this (notice the dependencies property):
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
}
Because your reference to Microsoft.NETCore.App was not inside dependencies it could not be found and your application was treated as a standalone app.
I've upgraded to Visual Studio Code 1.0.0 and I'm trying to retrofit an ASP.NET Core project I was working on in VSCode previously, before the new release. VSCode is seemingly quite different now in terms of configuration. I've worked with this tutorial and these samples. I'm having reasonable success getting the MVC6 aspect of my project compiling and working properly, but the EntityFramework 7 aspect is a no-go.
When I do a dotnet restore on my project, I get the following error:
Package EntityFramework.Core 7.0.0-rc1-final is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0).
I've been experimenting more-or-less randomly with the project.json in the hopes of finding a solution, but I don't seem to be making much progress. Is netcoreapp1.0 still just too new to be retro-compatible with EntityFramework? What options are available?
Here's my project.json, by the way. It's pretty much stock from the HelloMvcApi sample mentioned above, but with the addition of the EntityFramework.Core dependency:
{
"compilationOptions": {
"emitEntryPoint": true,
"debugType": "portable"
},
"dependencies": {
"Microsoft.AspNetCore.Mvc.Core": "1.0.0-*",
"Microsoft.AspNetCore.Mvc.Formatters.Json": "1.0.0-*",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-*",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*",
"Microsoft.Extensions.Logging.Console": "1.0.0-*",
"EntityFramework.Core": "7.0.0-rc1-final",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-*"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"portable-net45+wp80+win8+wpa81+dnxcore50"
]
}
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": {
"version": "1.0.0-*",
"imports": "portable-net45+wp80+win8+wpa81+dnxcore50"
}
},
"scripts": {
"postpublish": "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
}
}
As mentioned in this announcement of breaking changes in RC2 :
The EntityFramework.* packages and namespaces are changing to Microsoft.EntityFrameworkCore.*
So you'll just need to switch your reference to point to the updated version :
"Microsoft.EntityFrameworkCore": "1.0.0-*",