Learning .Net


.net is a dev platform, a collection of languages and libraries for building software.
.Net is a framework / ecosystem to build cross platform apps.
You can write code in C#, F#, VB and powershells under the .net framework
These get compiled to the same Intermediate Language (IL, dll files) and get executed by the common runtime. This makes it platform independent.

Platforms are the things you build software FOR:
.Net core: windows macos, linux
.net framework: web sites, services and apps on Windows
Xamarin/Mono: .net for mobile

Base class library(BCL) set of functions with basic necessities covered. Like Httpclient, Threading.Task...

Can build any app with .net: web, cli, mobile, games (unity)

Blazor is the web framework
loook more up on this and rewrite it better.
blazor assembly is for building front end client apps with html, css and c#. uses web assembly to translate operations to machine code. therefore soporta todas las plataformas.

hello world

Download dotnet from the site

With the 'dotnet' CLI create a project.
dotnet run environment=development runs in 'development' mode, if error occurs it returns the stack trace and all debug info to client.

Dotnet hello world tutorial
Dotnet docs



Model Binding

By default, model binding gets data in the form of key-value pairs from the following sources in an HTTP request:

  1. Form fields
  2. The request body (For controllers that have the [ApiController] attribute.)
  3. Route data
  4. Query string parameters
  5. Uploaded files


Entity Framework (ef)
install tool globally with dotnet tool install -g dotnet-ef

Code first approach

  1. create entity C# classes
  2. create migration dotnet ef migrations add InitialCreate
  3. run migration dotnet ef database update

Database First approach

generating Entity classes and DbContext classes from an existing database schema.
there are a few ways to do this

EF Core Power Tools

I can use the EF Core Power Tools plugin in VS. and generate Entities and DbContext class from there.


Scaffolding commands

Or I can run scaffolding commands
guide 1
guide 2(they basically show the same thing)
make sure to have these NuGet packages installed in the project:


package manager terminal command (in VS):

Scaffold-DbContext "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test_Invoice;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models 
Scaffold-DbContext "Server=(localdb)\MSSQLLocalDB;Database=Test_Invoice;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

dotnet CLI command (in regular shell):

dotnet ef dbcontext scaffold "Server=(localdb)\MSSQLLocalDB;Database=Test_Invoice;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models
Scaffold-DbContext "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test_Invoice;Integrated Security=True" Microsoft.EntityFrameWorkCore.SqlServer --output-dir Models --context TestInvoiceDbContext --context-dir Data --data-annotations --force

(localdb)\MSSQLLocalDB seems special syntax for LocalDB instances.
I can add the -f flag at the end of the command to force scaffolding to overwrite existing model files.

once the classes are created we can then / have to connect and manipulate our existing database. add / modify the DbContext to include the new generated classes

I can even generate the Db Context class.

You can then even generate the Controllers and Views automagically !!!!!

HTML templater

Razor (.cshtml, .razor files)

asp-for html attribute on inputs will automatically set the name, id and value html attributes with the correct values so the model binder can recognize the inputs.

inside asp-for I don't need to wrap variables in @()
in other non-razor attributes if I want to insert a variable value i Have to wrap it with @().

instead of using asp-for I could also manually set the html attributes that allow the Model Binder to recognize the form input.
The syntax is:

When dynamically adding Invoice.InvoiceDetails I had to manually add new markup with javascript and set the name, id and value html attributed to the form inputs. the values of these attributed were InvoiceDetails[${i}].Price, for example. the ${} syntax is for inserting var values in strings in javascript.
The model binder in the controller will find and bind these elements correctly. as long as their name and values attrs are set correctly.

project structure

appsettings.json has ConnectionString for database
Program.cs: entry point, where server and services get started