ASP.NET Databases Cheatsheet
ASP.NET Databases Cheatsheet
ASP.NET: Databases
Database Model
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 1/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
Database Context
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 2/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
DbSet Type
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 3/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
Entity Framework provides command-line tools that help dotnet ef migrations add InitialCreate
manage the connected database. Use these commands in
dotnet ef database update
the bash shell or Windows command prompt to create an
initial database file and schema. This will read the context
class and evaluate each database model represented by a
DbSet . The SQL syntax necessary to create all schema
objects is then generated and executed.
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 4/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
Saving Changes
The Entity Framework context DbSet member provides // Assuming Country is of type Country
the Attach() method to update an existing record, the
// Assuming _context is of a type
Add() method to insert a new record, and the
Remove() method to delete an existing record. Any inheriting DbSet
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");
}
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 5/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
Deleting Records
The Entity Framework context DbSet member provides // Assuming Country is of type Country
the Remove() method to delete an existing record from
// Assuming _context is of a type
the in-memory representation of the database table. Any
combination of multiple record deletions can be batched inheriting DbSet
before saving.
Use the EF context SaveChanges() or
public async Task<IActionResult>
SaveChangesAsync() methods to persist all deletions
to the database table. OnPostAsync(string id)
{
if (id == null)
{
return NotFound();
}
if (Country != null)
{
_context.Countries.Remove(Country);
}
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 6/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
LINQ Queries
DisplayNameFor Helper
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 7/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
Model Binding
Adding Records
The Entity Framework context DbSet member provides // Assuming Country is of type Country
the Add() and AddAsync() methods to insert a new
// Assuming _context is of a type
record into the in-memory representation of the
corresponding database table. A batch of multiple inheriting DbSet
records can also be added in this fashion.
The record is passed from the browser in the <form>
public async Task<IActionResult>
post back. In this case a Country member is declared
OnPostAsync(string id)
with a [BindProperty] attribute so the entire record is
passed back to the server. {
Use the EF context SaveChanges() or if (!ModelState.IsValid)
SaveChangesAsync() methods to persist all new
{
records to the database table.
return Page();
}
await
_context.Countries.AddAsync(Country);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 8/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
Finding Records
The Entity Framework context DbSet member provides // Assuming Country is of type Country
the Find() and FindAsync() methods to retrieve an
// Assuming _context is of a type
existing record from the in-memory representation of the
database table. Assign the result of this method to a local inheriting DbSet
member in the page model.
This method generates the appropriate SQL syntax
public async Task<IActionResult>
needed to access the record in the database table.
OnGetAsync(string id)
{
if (id == null)
{
return NotFound();
}
return Page();
}
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 9/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
Updating Records
The Entity Framework context DbSet member provides // Assuming Country is of type Country
the Attach() method to update an existing record in the
// Assuming _context is of a type
in-memory representation of the corresponding
database table. A batch of multiple records can also be inheriting DbSet
updated in this fashion.
The record is passed from the browser in the <form>
public async Task<IActionResult>
post back. In this case a Country member is declared
with a [BindProperty] attribute so the entire record is
OnPostAsync(string id)
passed back to the server. {
Use the EF context SaveChanges() or if (!ModelState.IsValid)
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");
}
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 10/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
return RedirectToPage("./Index");
}
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 11/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
<select asp-for="Country.ContinentI
using Microsoft.AspNetCore.Mvc.Rend
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 12/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
Validation Attribute
[Display] Attribute
[DisplayFormat] Attribute
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 13/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
[DataType] Attribute
[Required] Attribute
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 14/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
[RegularExpression] Attribute
[StringLength] Attribute
[Range] Attribute
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 15/16
2/24/25, 1:58 PM ASP.NET I: ASP.NET: Databases Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/asp-net-i/modules/asp-net-databases/cheatsheet 16/16