How can I install and use Entity Framework Core in Visual Studio Code? - asp.net-core-mvc

I have already created the initial asp.net mvc web application template in visual studio code.
And I can run the application in a browser at localhost:5000/
I can also see the views, controllers, viewmodels like the previous asp.net mvc core.
Now, how can I install and use entity framework core using visual studio code?
Thanks.

VS Code is just an editor. It doesn't install EF (or any packages). You can install EF by editing your csproj file to contain this set of lines.
<ItemGroup>
<PackageGroup Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.0" />
</ItemGroup>
VS Code may give you a prompt to "restore" pacakges. If not, call dotnet restore on command line.
Alternatively, on command line you can execute:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
See the "Getting Started" guide for more details on EF. To use EF, you need to write code. A full example is beyond the scope of a good StackOverflow answer and is subject to change as EF Core continually updates.

1st :
You need three line of code to install EntityFrameWork core and its dependencies.
2nd :
install them in this form
dotnet add package Microsoft.EntityFrameworkCore -v 2.1.14
dotnet add package Microsoft.EntityFrameworkCore.tools -v 2.1.14
dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 2.1.14
I put a sample of them with version but you can get latest entityframework version if you remove version in last of line.
like this.
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.tools
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
3rd
Remember your .Ner Core version should be compatible with lastest version of EntityFrameWork core which you want to install.So if you have error either you have to install lastet version of .Net Core or install EntityFrameWork core with a compatible version with .Net Core.
Probably you are looking for EntityFrameWork core version here and also for .net Core version here

To be able to use entity framework in visual code you can use yeoman on the terminal:
>npm install -g yo
>npm install -g generator-efrepo
>yo efrepo
Then follow the instructions

In addition to answers given above, following article has all the steps for adding EF Core using .Net Core CLI.
Getting Started with EF Core
Hope this helps to someone.
*Edit:
- VS Code users can execute dotnet commands from Terminal window (Ctrl+Shift+`)

I think this is the updated link, as of June 3.
https://docs.efproject.net/en/latest/

In my case i had deleted the "csproj" by accident, after research a lot, i knew about this possibility and found the deleted file in my trash.

Related

How to push an update to a NuGet feed for .NET Framework and .NET Core libraries

I have my own NuGet feed in Azure Artefacts. Currently, there are two packages in the feed:
INTLConfiguration.Client [Version 1.0.0]
INTLConfiguration.NetFramework.Client [Version 1.0.0]
The top one is a .NET Core library and the bottom one is a .NET Framework library.
I need to push an update to the NuGet feed for both versions to be 1.0.1 - but I'm having some trouble doing this. I packed 'INTLConfiguration.Client' and renamed the .nukpg from INTLConfiguration.Client.1.0.0.nupkg to INTLConfiguration.Client.1.0.1.nupkg and tried to push the nuget to my source feed but it errored with a conflict message saying v1.0.0 already exists.
How do I go about updating both of these nuget packages into my source feed?
Thank you.
I packed 'INTLConfiguration.Client' and renamed the .nukpg from
INTLConfiguration.Client.1.0.0.nupkg to
INTLConfiguration.Client.1.0.1.nupkg
It seems that your update is just to rename the output xxx.nupkg. It's not the valid way to create updated .nupkg. A xx.nupkg is something like a .zip. Renaming it from Name.nupkg to Name.zip and then you can unzip it and see its content. Open the ProjectName.nuspec and you can find the version defined in it is still 1.0.0.
My guess:
Maybe the way you use to pack is something like creating a .net core project and right-click the pack button which outputs a ProjectName.1.0.0.nupkg.
1# If so, the easiest way to resolve it is right-click Project name in Solution Explorer=>Properties and change the Package version there:
Change the version to 1.0.1 and pack it again.
2# Also we can use .nuspec file defined by us for this option.
Add a text file to the project and rename it to xxx.nuspec. Change its build action in property window to content.
Right-click the project=>unload the project=>edit the xxx.csproj file.
Add a script like below into it:
<PropertyGroup>
<NuspecFile>NuspecName.nuspec</NuspecFile>
</PropertyGroup>
Then reload the project, every time when we use Pack option it will call NuspecName.nuspec file.
To create a nuget package by command-line, I suggest you use dotnet.exe or nuget.exe.
For your .net core project, you can use dotnet pack command.Some details about it see here.
3# To create a .net core package with version 1.0.2 without using a .nuspec file:
Open cmd.exe, and type command: cd C:\PathToProjectFolder to navigate to ProjectDir(where exists xx.csproj)
Then type command like dotnet pack -p:PackageVersion=1.0.1 to create a really version-1.0.1 nuget package. (If you only have one .csproj in the dir)
Or you need to specify the .csproj like: dotnet pack ~/projects/app1/project.csproj -p:PackageVersion=1.0.1
4# To create a .net core package with version 1.0.2 using a .nuspec file:
Create a .nuspec file and modify its content to meet your needs(Version, AuthorName...).
Open cmd.exe and use a command like:dotnet pack ~/projects/app1/project.csproj /p:NuspecFile=~/projects/app1/project.nuspec /p:NuspecBasePath=~/projects/app1/nuget
If you use the Pack option(Right-click project=>Pack button) in VS, check 1# or 2#.
If you use command-line, you can check 3# or 4#. Hope it helps:)
Update:
How do I go about updating both of these nuget packages into my source
feed?
You can check this tutorial to create a package which targets .net framework.And update the version in .nuspec before you pack it. Also, if you want to get an updated nuget package, I suggest you update the assembly version and file version for the dll itself also.

ASP.NET Core Scaffolding does not work in VS 2017

I have a beginner ASP.NET Core project in Visual Studio 2017, and I am at the scaffolding step of the HelloWorld. The Scaffolding does not work, I tested on a first computer, then at a second one...
when I try to generate a controller with views
, it gives the following error on the first machine:
Microsoft Visual Studio
Error
There was an error running the selected code generator:
'Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.Web.CodeGeneration.Utils, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
at Microsoft.VisualStudio.Web.CodeGeneration.Design.Program.Main(String[] args)'
OK
On the second machine:
Microsoft Visual Studio
Error
There was an error running the selected code generator:
'Error: assembly specified in the dependencies manifest was not found -- package: 'microsoft.applicationinsights.aspnetcore', version: '2.1.0', path: 'lib/netstandard1.6/Microsoft.ApplicationInsights.AspNetCore.dll''
OK
The following works for me.
I deleted bin & obj folders, build the project again and it works.
In case if it gives you error: it couldn't find project.deps.json in debug folder.
Set project to debug, ran it and now you can add scaffolded items.
Clear local NuGet cache with the command from console:
dotnet nuget locals all --clear
Restore all Packages
Install (or update) NuGet package Microsoft.CodeAnalysis.CSharp.Workspaces version 2.0.0 (or higher) EDIT: It seams versions 2.3.0 and 2.3.1 cause some problems. So stick with 2.2.0 at most until MS solves this issues (https://github.com/dotnet/roslyn/issues/20873).
Restore all Packages again and try scaffolding.
That worked for me.
Be sure you have properly referenced these packages in your project:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
My projects uses Asp.Net Core 1.1.2 and VS2017 Community
I has the same situation. On .net core version 2.2 and with VS 2017 Enterprise.
The next instruction hepls for me:
open VS with Admin rights.
load solution to VS
add controller from scaffolding menu. VS will install 'Microsoft.VisualStudio.Web.CodeGeneration.Design' and 'Microsoft.VisualStudio.Web.CodeGeneration.Tools' packages.
close VS
open VS and load project. Add controllers via scaffloading is working fine
Maybe this instruction helps somebody,
Thanks.
For the benefit of searchers:
I got this in VS2019. The cause was that my code was broken.
Didn't realise why to start, but when you realise that code generation is a nuget package; if the project doesn't build, then Visual Studio can't get to that package.
Once I got the code compiling, this error went away.
you should change the version of Microsoft.VisualStudio.Web.CodeGeneration.Design exactly like the version of Microsoft.AspNetCore.App
On menu --> Compile --> Clean Solution
On menu --> Compile --> Build Solution.
Then repeat de creation of crud using scaffolding.
That works for me. Thanks
I was trying to run the command bellow with the version 3.0.0 of asp.net core installed, so I got the same error
Command: dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
Error:
Scaffolding failed.
Could not load file or assembly 'Microsoft.VisualStudio.Web.CodeGeneration, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
Solution:
dotnet nuget locals all --clear
dotnet remove package Microsoft.VisualStudio.Web.CodeGeneration.Utils
Change the PackageRefere for the version 2.2.0
Run the command again.
dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
Solution:
Go to VS2019 --> Dependencies -->Packages
Click on Manage NuGet
In the search type "Microsoft.VisualStudio.Web.CodeGeneration.Utils"
or the Dll that is missing.
Click Install.
Rebuild the solution
Then add the Scaffolding item.
It works..:-)
I fixed this by uninstalling all entity framework packages using nuget package manager and again reinstall with same version.
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
(3.10 version)
go to solution explorer -> clean solution then build solution
this worked for me.
I was receiving different error messages. Actually there was nothing specific, no mention of dependencies or assemblies. I tried all of the above suggestions with no success. Then I tried letting it create a new DbContext instead of specifying an existing one. That resolved my issue.

How do I get Ionide to see .net core Linq and AspNetCore namespaces?

I recently installed dotnet core 1.0.0-rc4-004771. After generating a new fsharp project with the following command: "dotnet new mvc -lang f#"
Ionide complains that it can't find the Linq and AspNetCore namespaces even though "dotnet build" builds just fine.
Has anyone else run into this issue?
should be fixed now, with
ionide >= 2.25.1
.net core sdk rtm (1.0.1)
see https://github.com/dotnet/netcorecli-fsc/wiki/.NET-Core-SDK-1.0.1 for more info
dotnet new mvc -lang f#
dotnet restore
code .
restore can be done after open code too, but is required to make intellisense work

Visual Studio Mac Preview Entity Framework SQLite add migration

I've installed Visual Studio for Mac (OSX 10.12.1) today and I've been diving in quite extensively.
I wanted to try to get EntityFrameworkCore (1.1.0) to run with SQLite.
So I've created a new Console Application .NET Core and with some troubles been able to add all the necessary nuget packages. Somehow Visual Studio was not able to download the dependencies, so I had to download every dependency manually. (Maybe this solves the problem: .Net Core 1.1.0 NuGet packages fail to install in Visual Studio Mac haven't testet this yet.)
As stated in this article (https://learn.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite) I wanted to add the migration, but I couldn't find the necessary command line tool in the IDE.
Did I miss something here?
Then I went on to use the .NET Core CLI to do it manually via. console. ( https://www.microsoft.com/net/core#macos). But when I execute dotnet ef migrations add init I get the following error.
No executable found matching command "dotnet-ef"
Was anyone able to get this to run successfully?
Visual Studio for Mac 2017 currently (April 2017) does not support adding a reference to Microsoft.EntityFrameworkCore.Tools.DotNet and returns an error:
Package 'Microsoft.EntityFrameworkCore.Tools.DotNet 1.0.0' has a package type 'DotnetCliTool' that is not supported by project 'MacMvc'.
You can edit the file manually and add the reference directly to the csproj file, as documented. Add this to your csproj file:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
Then run dotnet restore to install the package. After that, you will be able to use dotnet ef migrations add NameOfMigration and dotnet ef database update scripts as per documentation.
N.B.: you must be in the project directory when executing commands.
Also see suggestion feeedback for VS 2017 for Mac:
https://visualstudio.uservoice.com/forums/563332-visual-studio-for-mac/suggestions/17169425-add-sql-server-integration
https://visualstudio.uservoice.com/forums/563332-visual-studio-for-mac/suggestions/17138506-terminal-window
Using VS for Mac, adding those following lines into .csproj makes the migration work for me:
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild2-final" />
Packages will be automatically restored saving the .csproj from VS.
To run the "dotnet ef" command through the terminal, you need to be in the project directory, I mean not from the directory where the .sln file is, but from the lower level.
Note: Same trick with v1.0.1 of Tools.DotNet didn't work, I do not know why.
Check if you have this section in your project.json file and add it if it's missing.
"tools": {
"Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4"
}
This is valid for EF 1.1, previous version was using Microsoft.EntityFrameworkCore.Tools package
Without add this ItemGroup, can't add the dbcontext scaffold connection string use of cmd, so adding this in your project (Edit .csproj) first
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
For me this solved the issue on macOS, run this on any terminal:
dotnet tool install --global dotnet-ef

Add Nuget reference to VS project package from command line

Is it possible to add a NuGet reference to an existing VS project (csproj or jsprox) using some command line tool?
I would need a functionality like package manager console offers using Install-Package command:
PM>Install-Package
This is not supported with NuGet.exe. With NuGet.exe you can download the NuGet packages based on what is in the packages.config file. You can also update NuGet packages and have their references updated in the project file by using NuGet.exe update. However you cannot use NuGet.exe to install the NuGet package so it adds the required references to the project file.
It is supported with Paket however if you use Paket then you would need to switch to using Paket for all NuGet packages since it has its own way of referencing the NuGet packages which does not include using the packages.config file. It also does not support PowerShell scripts.
I looked at installing NuGet Packages from the command line outside of Visual Studio using SharpDevelop and a set of PowerShell commands. This was a proof of concept but is not supported and requires most of SharpDevelop to be available.
If you are using .NET Core there is now a way to achieve this using the dotnet CLI.
dotnet add package EntityFramework
See Steve Smith's blog post for more information.

Resources