Skip to content

Commit f9db636

Browse files
Merge pull request #2890 from dotnet/main
✅ Merge `main` into `live`
2 parents afbf3ea + 76ba4e4 commit f9db636

5 files changed

+994
-4
lines changed

docs/caching/stackexchange-redis-distributed-caching-integration.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ You can also set up the [ConfigurationOptions](https://stackexchange.github.io/S
156156
```csharp
157157
builder.AddRedisDistributedCache(
158158
"cache",
159-
static settings => settings.ConnectTimeout = 3_000);
159+
null,
160+
static options => options.ConnectTimeout = 3_000);
160161
```
161162

162163
[!INCLUDE [redis-distributed-client-health-checks-and-diagnostics](includes/redis-distributed-client-health-checks-and-diagnostics.md)]

docs/database/entity-framework-core-integration-overview.md

+83-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ zone_pivot_groups: entity-framework-client-integration
88

99
# Entity Framework Core overview
1010

11-
In a cloud-native solution, such as those .NET Aspire is built to create, microservices often need to store data in relational databases. .NET Aspire includes integrations that you can use to ease that task, some of which use the Entity Framework Core(EF Core) object-relational mapper (O/RM) approach to streamline the process.
11+
In a cloud-native solution, such as those .NET Aspire is built to create, microservices often need to store data in relational databases. .NET Aspire includes integrations that you can use to ease that task, some of which use the Entity Framework Core (EF Core) object-relational mapper (O/RM) approach to streamline the process.
1212

13-
Developers use O/RMs to work with databases using code objects instead of SQL queries. EF Core automatically codes database interactions by generating SQL queries based on LINQ queries. EF Core supports various database providers, including SQL Server, PostgreSQL, and MySQL, so it's easy to interact with relational databases while following object-oriented principles.
13+
Developers use O/RMs to work with databases using code objects instead of SQL queries. EF Core automatically codes database interactions by generating SQL queries based on Language-Integrated Query (LINQ) queries. EF Core supports various database providers, including SQL Server, PostgreSQL, and MySQL, so it's easy to interact with relational databases while following object-oriented principles.
1414

1515
The most commonly used .NET Aspire EF Core client integrations are:
1616

@@ -20,8 +20,88 @@ The most commonly used .NET Aspire EF Core client integrations are:
2020
- [PostgreSQL Entity Framework Core integration](postgresql-entity-framework-integration.md)
2121
- [SQL Server Entity Framework Core integration](sql-server-entity-framework-integration.md)
2222

23+
## Overview of EF Core
24+
25+
O/RMs create a model that matches the schema and relationships defined in the database. Code against this model to query the data, create new records, or make other changes. In EF Core the model consists of:
26+
27+
- A set of entity classes, each of which represents a table in the database and its columns.
28+
- A context class that represents the whole database.
29+
30+
An entity class might look like this:
31+
32+
```csharp
33+
using System.ComponentModel.DataAnnotations;
34+
35+
namespace SupportDeskProject.Data;
36+
37+
public sealed class SupportTicket
38+
{
39+
public int Id { get; set; }
40+
[Required]
41+
public string Title { get; set; } = string.Empty;
42+
[Required]
43+
public string Description { get; set; } = string.Empty;
44+
}
45+
```
46+
47+
This entity class represents a database table with three columns: **Id**, **Title**, and **Description**.
48+
49+
A context class must inherit from <xref:System.Data.Entity.DbContext> and looks like this:
50+
51+
```csharp
52+
using Microsoft.EntityFrameworkCore;
53+
using System.Reflection.Metadata;
54+
55+
namespace SupportDeskProject.Data;
56+
57+
public class TicketContext(DbContextOptions options) : DbContext(options)
58+
{
59+
public DbSet<SupportTicket> Tickets => Set<SupportTicket>();
60+
}
61+
```
62+
63+
This context represents a database with a single table of support tickets. An instance of the context is usually created for each unit of work in the database. For example, a unit of work might be the creation of a new customer, and require changes in the Customers and Addresses tables. Once the unit of work is complete, you should dispose of the context.
64+
65+
> [!NOTE]
66+
> For more information about creating models in EF Core, see [Creating and Configuring a Model](/ef/core/modeling/) in the EF Core documentation.
67+
68+
Once you've created a model, you can use LINQ to query it:
69+
70+
```csharp
71+
using (var db = new TicketContext())
72+
{
73+
var tickets = await db.Tickets
74+
.Where(t => t.Title = "Unable to log on")
75+
.OrderBy(t => t.Description)
76+
.ToListAsync();
77+
}
78+
```
79+
2380
> [!NOTE]
24-
> In .NET Aspire, EF Core is implemented by client integrations, not hosting integrations. So, for example, to use EF Core with a SQL Server database, you'd use the SQL Server hosting integration to create the SQL Server container and add a database to it. In the consuming microservices, when you want to use EF Core, choose the SQL Server Entity Framework Core integration instead of the SQL Server client integration.
81+
> EF Core also supports creating, modifying, and deleted records and complex queries. For more information, see [Querying Data](/ef/core/querying/) and [Saving Data](/ef/core/saving/)
82+
83+
## How .NET Aspire can help
84+
85+
.NET Aspire is designed to help build observable, production-ready, cloud-native solutions that consist of multiple microservices. It orchestrates multiple projects, each of which may be a microservice written by a dedicated team, and connects them to each other. It provides integrations that make it easy to connect to common services, such as databases.
86+
87+
If you want to use EF Core in any of your microservices, .NET Aspire can help by:
88+
89+
- Managing the database container, or a connection to an existing database, centrally in the App Host project and passing its reference to any project that uses it.
90+
91+
> [!IMPORTANT]
92+
> In .NET Aspire, EF Core is implemented by client integrations, not hosting integrations. The centralized management of the database in the App Host doesn't involve EF Core, which runs in consuming microservice projects instead. For more information, see [Cosmos DB Hosting integration](/dotnet/aspire/database/azure-cosmos-db-entity-framework-integration#hosting-integration), [MySQL Pomelo Hosting integration](/dotnet/aspire/database/mysql-entity-framework-integration#hosting-integration), [Oracle Hosting integration](/dotnet/aspire/database/oracle-entity-framework-integration#hosting-integration), [PostgreSQL Hosting integration](/dotnet/aspire/database/postgresql-entity-framework-integration#hosting-integration), or [SQL Server Hosting integration](/dotnet/aspire/database/sql-server-entity-framework-integration#hosting-integration).
93+
94+
- Providing EF Core-aware integrations that make it easy to create contexts in microservice projects. There are EF Core integrations for SQL Server, MySQL, PostgreSQL, Oracle, Cosmos DB, and other popular database systems.
95+
96+
To use EF Core in your microservice, you must:
97+
98+
- Define the EF Core model with entity classes and context classes.
99+
- Create an instance of the data context, using the reference passed from the App Host, and add it to the Dependency Injection (DI) container.
100+
- When you want to interact with the database, obtain the context from DI and use it to execute LINQ queries against the database as normal for any EF Core code.
101+
102+
:::image type="content" source="media/ef-core-aspire-architecture-thumb.png" lightbox="media/ef-core-aspire-architecture-large.png" alt-text="A diagram showing how .NET Aspire utilizes EF Core to interact with a database.." :::
103+
104+
Both defining the EF Core model and querying the database are the same in .NET Aspire projects as in any other EF Core app. However, creating the data context differs. In the rest of this article, you'll learn how to create an configure EF Core contexts in .NET Aspire project.
25105

26106
## Use .NET Aspire to create an EF Core context
27107

Loading
Loading

0 commit comments

Comments
 (0)