Learning .Net
overview
.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.
Commands
new
run
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
Tooling
- NuGet is the pkg manager
dotnet
CLI
for building, running, watching, running tooling... the project.
Libs
AspNetCore
Controllers
Model Binding
Sources
By default, model binding gets data in the form of key-value pairs from the following sources in an HTTP request:
- Form fields
- The request body (For controllers that have the [ApiController] attribute.)
- Route data
- Query string parameters
- Uploaded files
ORM
Entity Framework (ef)
install tool globally with dotnet tool install -g dotnet-ef
Code first approach
- create entity C# classes
- create migration
dotnet ef migrations add InitialCreate
- 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.
https://stackoverflow.com/a/74246101/14318987
ADO.NET Entity Data Model
https://learn.microsoft.com/en-us/ef/ef6/modeling/designer/workflows/database-first
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:
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
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.
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/generating-views
You can then even generate the Controllers and Views automagically !!!!!
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/generating-views
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:
- verify the class attribute names of the imported
@model
on top of the razor file. - Use those same names to set the html attributes.
so If I have aUser
class with attributesId
andName
that is used at the model of my Razor page. I would have to set the html attribute toname="Id"
andname="Name"
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
bundleconfig.json
:
Program.cs
: entry point, where server and services get started
Startup.cs
:
Properties/launchSettings.json
:
src/