Creating classes from Postgres database for Entity Framework Core - asp.net-core

Looking for a method to automatically create entity classes from existing Postgres 13 database.
in
According to
http://www.npgsql.org/efcore/index.html
In Visual Studio 2019 command prompt
dotnet ef dbcontext scaffold "Host=my_host;Database=my_db;Username=my_user;Password=my_pw" Npgsql.EntityFrameworkCore.PostgreSQL
Command should do this but it throws error
Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET program, but dotnet-ef does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
NpgSql.EntityFrameworkCore.PostgreSQL v5.0.1 package is installed from Nuget.

You should install Dotnet EF tool.
try this in your package manager console
dotnet tool install --global dotnet-ef
After that it is recommended to install design tool aswell so you can use tools on specific project
dotnet add package Microsoft.EntityFrameworkCore.Design

Related

In ASP.NET MVC Core, what do the Microsoft.AspNetCore.Razor.Design and Microsoft.VisualStudio.Web.CodeGeneration.Design packages do?

Are they only needed at design time? Can they be removed without causing any build issues? (Targeting framework netcoreapp2.1).
The package of Microsoft.AspNetCore.Razor.Design contains MSBuild support for Razor. If you're developing an asp.net core app, there's no need to add an package reference on Microsoft.AspNetCore.Razor.Design manually (and also you're not supposed to remove them manually). Because it is referenced by Microsoft.AspNetCore.App meta package, which means if you have a dependency on Microsoft.AspNetCore.App, you'll reference the package automatically.
The package of Microsoft.VisualStudio.Web.CodeGeneration.Design is a quite different one. As the name suggests, it is used to generate codes only. For example, you want to develop a project without Visual Studio:
You create a new project using the command of dotnet new mvc
And then you create a new Model named App.Models.MyModel mannually
Typically, we'll use Visual Studio to generate controllers, views, DbContext and so on. But what if we don't have Visual Studio?
If we have the Microsoft.VisualStudio.Web.CodeGeneration.Design referenced in our *.csproj file, we can create the CRUD scaffold using the following command :
dotnet aspnet-codegenerator controller -m $model -dc $dcClass -name $controllerName -namespace $controllerNamespace -outDir Controllers --useDefaultLayout
There're also some other sub commands such as :
dotnet aspnet-codegenerator identity -dc $dcClass
However, this package is only used to scaffold. Once you have the codes generated, you can feel free to remove this package before publish.
As a side note, to use the dotnet aspnet-codegenerator command, we should first install the tool:
dotnet tool install --global dotnet-aspnet-codegenerator

.NET Standard and TFS 2015 -> Build failures

My infrastructure now has TFS 2015, but we started a new project in .NET Standard. Our build server now has VS2017, and the project builds when loaded within VS2017 in that server.
When we set a new build definition to run the build through the build agent, then it fails. Seems even that System is not found:
Error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
Is there any simple thing to do to make it work, ir we will have to migrate to the new build steps to make it work? Some workaround?
I find a workaround for that. Simply I gave up of using the TFS 2015 build steps MSBUILD and Visual Studio Build, and now I am using the Command Line step. Calling the commands from there:
dotnet restore
dotnet build
That does the trick.
This is because we need to do our restore a different way using the .net core restore. You could fix this by adding the .NET Core (PREVIEW) task to the build definition or just use command line task. With command line task could running dotnet restore, dotnet build, dotnet publish, and dotnet test.
More details please take a look at this blog: Setting up .net core continuous integration build with VSTS/TFS
For command line solution please refer vsts-agent Build Definition for .NET Core (with Test Results) Also take a look at this similar question: Visual studio team services build .net core 1.1

Where's NuGet manager console in Rider IDE?

I am new to .NET platform. From time to time, I had problems with Visual Studio and I decided to use Rider. It encouraged me again (I have been using JetBrains products for 2 years). But I can not find the NuGet console (which is so called in Visual Studio).
Where is it?
It's not accessible yet. Please vote this issue https://youtrack.jetbrains.com/issue/RIDER-435
It is available since Rider 2018.1.
Please see JetBrains' blog post about installation: https://blog.jetbrains.com/dotnet/2018/04/06/entity-framework-support-rider-2018-1/
P.S. tested on Rider 2020.2.1.
You Can use PowerShell on Windows "Where Your DbContext is" Like this:
dotnet ef
dotnet ef migrations add [Name]
If you need to run entity framework commands such as
dotnet ef migrations add "initialSetup"
Then go to your terminal Alt+f12 or double-shift and type “terminal”.
Then run:
dotnet tool install --global dotnet-ef
And then (making sure you're in your project directory):
dotnet ef migrations add "initialSetup"
Although Rider has now added the UI for migration, unfortunately, if you use the latest dotnet feature the UI doesn't work.
For example, I created the project without Startup class file, the UI won't let me continue:
But I tried these commands which works:
dotnet tool install --global dotnet-ef
# You can type the DbContext manually now!
dotnet ef migrations add 'initial' --project src/IdentityServer/IdentityServer.csproj --context PersistedGrantDbContext
Then I execute:
dotnet ef database update --project src/IdentityServer/IdentityServer.csproj --context ConfigurationDbContext
Now the database updated with all the tables there:
They haven't implemented the Package manager console yet. So you will have to use Visual studio for that
You can use terminal toolwindow. Or NuGet manager (GUI)...

Creating a nuget package containing a custom build step for .NET Core

Is it possible to create a nuget package containing a custom build step (analyzing the generated assembly and automatically generating some metadata)? The target environment should be .NET Core with the new .csproj format (Currently using Visual Studio 2017 RC). Best case would be if this only adds a msbuild step and no dependency to the projects resulting assembly.
If seen that it should be possible with full .NET, but the documentation for msbuild and the new .csproj for .NET Core is a bit sparse at the moment.
You can add MSBuild targets to a NuGet package in the "build" folder. The targets or props file name must match the package ID. When installed via "PackageReference" or packages.config, NuGet will automatically import the targets/props file into the csproj, so these files must be written as valid MSBuild code.
i.e. if your package was named "MyClassLibrary", you could have the following layout:
(package root)
lib/
netstandard1.3/
MyClassLibrary.dll
build/
netstandard1.3/
MyClassLibrary.props
MyClassLibrary.targets
For example, see Microsoft.Extensions.Configuration.UserSecrets on NuGet.org or and its source code on GitHub. This package uses an MSBuild target to automatically generate an assembly attribute before the compilation step
Nuget may execute "init.ps1" powershell script during installation if find it in your nuget package. But keep attention, that script support was modified in Nuget 3.x:
In Nuget 3.xx Powershell script support was modified to no longer execute install and uninstall scripts (install.ps1, uninstall.ps1), but init scripts are still executed.
Additionally, running NuGet outside of Visual Studio at the command-line or on other operating systems cannot execute these Powershell scripts as they relied on Visual Studio automation. Powershell is in NuGet to provide a shim to allow for missing features to run inside of Visual Studio
Powershell script support was modified to no longer execute install and uninstall scripts, but init scripts are still executed.
You can find more here in "Powershell Install and Uninstall Scripts".

Building asp.net core in Teamcity failing with dependencies

I am new to the DevOPs field. I am trying to build a .netcore project using teamcity which is failing with dependencies errors. The dependency System.Diagnostics.Contracts >= 4.0.1 could not be resolved. The dependency System.Net.WebSockets >= 4.0.0 could not be resolved. The dependency System.Runtime.Serialization.Primitives >= 4.1.1 The dependency System.Text.Encodings.Web >= 4.0.0 could not be resolved. I talked to one of the colleagues and he suggested to install the nugget packages but the solution is building on my and the devs system but is failing by Teamcity. how can I install the dependencies on teamcity. In Asp.net the nugget package management is done automatically(some one told me) So how to do it in .netcore.
found out that the terminology is called package restore and in dotnet core it is done by running
dotnet restore
from the directory where the project.json is located or the directory can be passed an an argument as well. After the restore there is no need to run msbuild. build can be done using
dotnet build
The new problem that I am facing now is that after the build and publishing the output folder to Azure web app I am unable to run my dot net core project as to run the project I need to run the command
dotnet nameoftheproject.dll
but I dont know how to run this command using teamcity after publish.