When a DbContext
is created in Entity Framework Core (EF Core), it provides a comprehensive set of methods for performing CRUD (Create, Read, Update, Delete) operations. These operations are primarily facilitated through the DbSet<TEntity>
property of the DbContext
. Below is an overview of the key methods available for each CRUD operation:
-
Add(TEntity entity)
: Adds the specified entity to the context in the Added state, which will be inserted into the database upon callingSaveChanges()
. -
AddRange(IEnumerable<TEntity> entities)
: Adds a collection of entities into the context, each marked as Added, useful for inserting multiple records at once.
-
Find(object[] keyValues)
: Finds an entity with the given primary key values, returning it without a database call if it's already being tracked by the context. -
Where(Expression<Func<TEntity, bool>> predicate)
: Uses LINQ to query entities from the database based on a predicate (not directly aDbSet<TEntity>
method but commonly used with it). -
FirstOrDefault(Expression<Func<TEntity, bool>> predicate)
: Finds the first entity matching the predicate, or returnsnull
if no match is found. -
ToListAsync()
: An extension method that asynchronously executes a query, returning the results as aList<TEntity>
.
-
Update(TEntity entity)
: Marks the entity as Modified, with EF Core generating an SQL UPDATE statement upon callingSaveChanges()
. -
UpdateRange(IEnumerable<TEntity> entities)
: Marks a collection of entities as Modified, useful for updating multiple records at once.
-
Remove(TEntity entity)
: Marks the entity as Deleted, with EF Core generating an SQL DELETE statement upon callingSaveChanges()
. -
RemoveRange(IEnumerable<TEntity> entities)
: Marks a collection of entities as Deleted, useful for deleting multiple records at once.
-
SaveChanges()
: Persists all changes made in the context to the database, automatically generating the necessary SQL statements for all Added, Modified, and Deleted entities. -
SaveChangesAsync()
: Asynchronously saves all changes made in the context to the database.
These methods, part of the DbContext
and DbSet<TEntity>
classes, enable developers to perform data access operations efficiently. EF Core also supports advanced features like transactions, concurrency handling, and custom SQL queries for complex scenarios.
The Results
class in ASP.NET Core provides a comprehensive set of factory methods for creating various types of HTTP responses. These methods simplify the process of returning common HTTP response types and status codes in minimal APIs. Here's an overview of some key methods provided by the Results
class, alongside NotFound
and Ok
:
- Produces an HTTP 400 (Bad Request) status code response. Can include an error object in the response body.
- Returns a result with a custom content string. Allows for an optional content type and defaults to an HTTP 200 (OK) status code.
- Generates an HTTP 201 (Created) status code, indicating a new resource was created. Includes a
Location
header and optionally the resource in the response body.
- Returns an HTTP 204 (No Content) status code, indicating a successful request with no content to return.
Json(object? data, JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null)
- Serializes the provided data object to JSON for the response body. Allows specifying content type, status code, and serialization options.
- Produces an HTTP 302 (Found) or 301 (Moved Permanently) status code for redirects. The
permanent
flag determines the specific status code.
- Generates an HTTP 401 (Unauthorized) status code, indicating missing or invalid authentication credentials.
- Returns an HTTP 403 (Forbidden) status code, indicating the server refuses to authorize the request.
- Initiates an authentication challenge, typically used when authentication is required and the application wants to trigger the process.
Problem(string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null)
- Produces a problem details object for conveying error details in a machine-readable format. Often used with the
application/problem+json
media type.
These methods facilitate handling various outcomes and HTTP status codes in a standardized and concise manner, enhancing the development experience with minimal APIs in ASP.NET Core.