Professional Documents
Culture Documents
Databases
Databases
ASP.NET: Databases
Database Model
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 1/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
Database Context
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 2/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
DbSet Type
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 3/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
Entity Framework provides command-line tools that dotnet ef migrations add InitialCreate
help manage the connected database. Use these
commands in the bash shell or Windows command
dotnet ef database update
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/learn-asp-net/modules/asp-net-databases/cheatsheet 4/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
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 new
inheriting DbSet
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");
}
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 5/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
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 record inheriting DbSet
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");
}
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 6/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
LINQ Queries
DisplayNameFor Helper
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 7/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
Model Binding
Adding Records
The Entity Framework context DbSet member // Assuming Country is of type Country
provides the Add() and AddAsync() methods
// Assuming _context is of a type
to insert a new record into the in-memory
representation of the corresponding database table. A inheriting DbSet
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");
}
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 8/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
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. Assign inheriting DbSet
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();
}
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 9/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
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 multiple inheriting DbSet
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
return Page();
updated records to the database table.
}
_context.Attach(Country).State =
EntityState.Modified;
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 10/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
<select asp-for="Country.Continen
using Microsoft.AspNetCore.Mvc.Re
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 11/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
Validation Attribute
[Display] Attribute
[DisplayFormat] Attribute
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 12/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
[DataType] Attribute
[Required] Attribute
[RegularExpression] Attribute
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 13/14
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Databases Cheatsheet | Codecademy
[StringLength] Attribute
[Range] Attribute
Print Share
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-databases/cheatsheet 14/14