Learn ASP - NET: ASP - NET: Databases Cheatsheet
Learn ASP - NET: ASP - NET: Databases Cheatsheet
NET
ASP.NET: Databases
Database Model
Entity Framework uses C# classes to de!ne the using System;
database model. This is an in-memory
representation of data stored in a database table.
Several model classes combine to form the schema public class Country
for the database. {
Each property maps to a column in a database table.
The bottom line in the example shows a type of
public string ID { get; set; }
Continent which implies a relationship to public string ContinentID { get; set;
another table. }
public string Name { get; set; }
public int? Population { get; set; }
public int? Area { get; set; }
public DateTime? UnitedNationsDate {
get; set; }
Database Context
The Entity Framework database context is a C# using Microsoft.EntityFrameworkCore;
class that provides connectivity to an external
database for an application. It relies on the
Microsoft.EntityFrameworkCore public class CountryContext : DbContext
library to de!ne the DB context which maps model {
entities to database tables and columns.
public
The DbContextOptions are injected into the
context class via the constructor. The options allow CountryContext(DbContextOptions<Country
con!guration changes per environment so the Context> options)
Development DB is used while coding and testing
: base(options)
but the Production DB would be referenced for real
work. {
The DbSet is an in-memory representation of a }
table or view which has a number of member
methods that can return a List<T> of records
public DbSet<Country> Countries {
or a single record.
get; set; }
public DbSet<Continent> Continents {
get; set; }
DbSet Type
The Entity Framework type DbSet represents a using Microsoft.EntityFrameworkCore;
database table in memory. It is typically used with a
<T> quali!er. The type, or T , is one of your
database model classes. The ModelBuilder
public class CountryContext : DbContext
binds each database table entity to a corresponding {
DbSet . public
DbSet has a number of member methods that CountryContext(DbContextOptions<Country
can return a List<T> of records or a single
record.
Context> options)
: base(options)
{
}
Model Binding
In ASP.NET Core, model binding is a feature that
simpli!es capturing and storing data in a web app.
The process of model binding includes retrieving
data from various sources, converting them to
collections of .NET types, and passing them to
controllers/page models. Helpers and attributes are
used to render HTML with the contents of bound
page models. Client- and server-side validation
scripts are used to ensure integrity during data
entry.
Adding Records
The Entity Framework context DbSet member // Assuming Country is of type Country
provides the Add() and AddAsync()
// Assuming _context is of a type
methods to insert a new record into the in-memory
representation of the corresponding database table. inheriting DbSet
A batch of multiple records can also be added in this
fashion.
public async Task<IActionResult>
The record is passed from the browser in the
<form> post back. In this case a Country OnPostAsync(string id)
member is declared with a [BindProperty] {
attribute so the entire record is passed back to the if (!ModelState.IsValid)
server.
{
Use the EF context SaveChanges() or
SaveChangesAsync() methods to persist all return Page();
new records to the database table. }
await
_context.Countries.AddAsync(Country);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
Saving Changes
The Entity Framework context DbSet member // Assuming Country is of type Country
provides the Attach() method to update an
// Assuming _context is of a type
existing record, the Add() method to insert a
inheriting DbSet
new record, and the Remove() method to
delete an existing record. Any combination of
multiple records can batched before saving. public async Task<IActionResult>
Use the EF context SaveChanges() or
OnPostAsync(string id)
SaveChangesAsync() methods to persist all
{
inserted, updated, and deleted records to the
database table. // update
_context.Attach(Country).State =
EntityState.Modified;
// insert
await
_context.Countries.AddAsync(Country);
// delete
Country Country = await
_context.Countries.FindAsync(id);
if (Country != null)
{
_context.Countries.Remove(Country);
}
return RedirectToPage("./Index");
}
Finding Records
The Entity Framework context DbSet member // Assuming Country is of type Country
provides the Find() and FindAsync()
// Assuming _context is of a type
methods to retrieve an existing record from the in-
memory representation of the database table. inheriting DbSet
Assign the result of this method to a local member in
the page model.
public async Task<IActionResult>
This method generates the appropriate SQL syntax
needed to access the record in the database table. OnGetAsync(string id)
{
if (id == null)
{
return NotFound();
}
return Page();
}
Deleting Records
The Entity Framework context DbSet member // Assuming Country is of type Country
provides the Remove() method to delete an
// Assuming _context is of a type
existing record from the in-memory representation
of the database table. Any combination of multiple inheriting DbSet
record deletions can be batched before saving.
Use the EF context SaveChanges() or public async Task<IActionResult>
SaveChangesAsync() methods to persist all
OnPostAsync(string id)
deletions to the database table.
{
if (id == null)
{
return NotFound();
}
if (Country != null)
{
_context.Countries.Remove(Country);
}
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
Updating Records
The Entity Framework context DbSet member // Assuming Country is of type Country
provides the Attach() method to update an
// Assuming _context is of a type
existing record in the in-memory representation of
the corresponding database table. A batch of inheriting DbSet
multiple records can also be updated in this fashion.
The record is passed from the browser in the
public async Task<IActionResult>
<form> post back. In this case a Country
OnPostAsync(string id)
member is declared with a [BindProperty]
attribute so the entire record is passed back to the {
server. if (!ModelState.IsValid)
Use the EF context SaveChanges() or
{
SaveChangesAsync() methods to persist all
updated records to the database table.
return Page();
}
_context.Attach(Country).State =
EntityState.Modified;
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
Validation Attribute
The asp-for attribute in an <input> <div>
element will render HTML and JavaScript that
<label asp-for="Continent.Name">
handle the display and data entry for a !eld based
on the model annotations. The JavaScript will set </label>
the valid )ag on the !eld. <div>
The asp-validation-for attribute in a
<input asp-for="Continent.Name" />
<span> element will display any error message
<span asp-validation-
generated when the property annotations are not
valid. for="Continent.Name"></span>
In this example, the <span> be rendered as this </div>
HTML:
</div>
<span class="field-validation-valid
[Display] Attribute
The [Display] attribute speci!es the caption using
for a label, textbox, or table heading.
System.ComponentModel.DataAnnotations;
Within a Razor Page, the
@Html.DisplayForName() helper defaults
to the property name unless the [Display] public class Country
attribute overrides it. In this case, Continent is {
displayed instead of the more technical
[Display(Name = "Continent")]
ContinentID .
public string ContinentID { get; set;
}
}
[DisplayFormat] Attribute
The [DisplayFormat] attribute can explicitly using
apply a C# format string. The optional
System.ComponentModel.DataAnnotations;
ApplyFormatInEditMode means the format
should also apply in edit mode.
[DisplayFormat] is often used in public class Country
combination with the [DataType] attribute. {
Together they determine the rendered HTML when
[DisplayFormat(DataFormatString = "
using the @Html.DisplayFor() helper.
{0:N0}", ApplyFormatInEditMode = true)]
public int? Population { get; set; }
}
[DataType] Attribute
The [DataType] attribute speci!es a more using
speci!c data type than the database column type. In
System.ComponentModel.DataAnnotations;
this case, the database table will use a
DateTime column but the render logic will only
show the date. public class Country
The @Html.DisplayFor() helper knows {
about types and will render a default format to
match the type. In this case, the HTML5 browser
[DataType(DataType.Date)]
date picker will appear when editing the !eld. [DisplayFormat(DataFormatString = "
{0:yyyy-MM-dd}", ApplyFormatInEditMode
= true)]
public DateTime? UnitedNationsDate {
get; set; }
}
[Required] Attribute
The [Required] attribute can be applied to using
one or more properties in a database model class. System.ComponentModel.DataAnnotations;
EF will create a NOT NULL column in the
database table for the property.
The client-side JavaScript validation scripts will public class Country
ensure that a non-empty string or number is valid {
before posting the record from the browser on
[Required]
inserts and updates.
public string Name { get; set; }
}
[RegularExpression] Attribute
The [RegularExpression] attribute can using
apply detailed restrictions for data input. The match
System.ComponentModel.DataAnnotations;
expression is evaluated during data entry and the
result returns true or false. If false, the model state
will not be valid and the optional public class Country
ErrorMessage will display. {
In a Razor page, the @Html.DisplayFor()
[RegularExpression(@"[A-Z]+",
helper only shows the data in the !eld. The asp-
validation-for attribute on a <span> tag ErrorMessage = "Only upper case
displays the ErrorMessage . characters are allowed.")]
public string CountryCode { get; set;
}
}
[StringLength] Attribute
The [StringLength] attribute speci!es the using
maximum length of characters that are allowed in a
System.ComponentModel.DataAnnotations;
data !eld and optionally the minimum length. The
model will not be )agged as valid if these restrictions
are exceeded. In this case, the ContinentID public class Country
must be exactly 2 characters in length.
{
In a Razor Page, the @Html.DisplayFor()
helper only shows the data in the !eld. The client-
[StringLength(2, MinimumLength = 2)]
side JavaScript validation scripts use the asp- public string ContinentCode { get;
validation-for attribute on a <span> tag set; }
to display a default error message.
}
[Range] Attribute
The [Range] attribute speci!es the minimum using
and maximum values in a data !eld. The model will
System.ComponentModel.DataAnnotations;
not be )agged as valid if these restrictions are
exceeded. In this case, Population must be
greater than 0 and less than the big number! public class Country
In a Razor page, the @Html.DisplayFor() {
helper only shows the data in the !eld. The client-
side JavaScript validation scripts use the asp- [Range(1, 10000000000)]
validation-for attribute on a <span> tag public int? Population { get; set; }
to display a default error message.
}
<select asp-for="Country.ContinentID
using Microsoft.AspNetCore.Mvc.Rendering;
DisplayNameFor Helper
The @Html.DisplayNameFor() tag helper <!-- In .cshtml file -->
is used to display the friendly name for a property in
<div>
a database model. By default, this will match the
property name. If a [Display(Name = @Html.DisplayNameFor(model =>
"Code")] annotation is applied to the property model.Continent.ID)
in the model class, that string is used instead </div>