इसे छोड़कर कंटेंट पर जाएं

Get started with the SQL Server Entity Framework Core integrations

यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।

SQL Server logo

SQL Server is a relational database management system developed by Microsoft. The Aspire SQL Server Entity Framework Core (EF Core) integration enables you to connect to existing SQL Server instances or create new instances from .NET with the mcr.microsoft.com/mssql/server container image.

In this introduction, you’ll see how to install and use the Aspire SQL Server EF Core integrations in a simple configuration. If you already have this knowledge, see the links below for full reference details.

To begin, install the Aspire SQL Server Hosting integration in your Aspire AppHost project. This integration allows you to create and manage SQL Server database instances from your Aspire hosting projects:

Aspire CLI — Aspire.Hosting.SqlServer पैकेज जोड़ें
aspire add sqlserver

Aspire CLI इंटरैक्टिव है; प्रॉम्प्ट पर उपयुक्त परिणाम चुनें:

Aspire CLI — उदाहरण आउटपुट
Select an integration to add:
> sqlserver (Aspire.Hosting.SqlServer)
> Other results listed as selectable options...

Next, in the AppHost project, create instances of SQL Server server and database resources, then pass the database to the consuming client projects:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql");
var sqldb = sql.AddDatabase("sqldb");
var exampleProject = builder.AddProject<Projects.ExampleProject>("exampleproject")
.WaitFor(sqldb)
.WithReference(sqldb);
builder.Build().Run();

Now that the hosting integration is ready, the next step is to install and configure the Aspire SQL Server EF Core client integration in any projects that need to use it.

In each of these consuming client projects, install the Aspire SQL Server EF Core client integration:

.NET CLI — Add Aspire.Microsoft.EntityFrameworkCore.SqlServer package
dotnet add package Aspire.Microsoft.EntityFrameworkCore.SqlServer

In the Program.cs file of your client-consuming project, call the AddSqlServerDbContext extension method on any IHostApplicationBuilder to register a DbContext for use via the dependency injection container. The method takes a connection name parameter.

C# — Program.cs
builder.AddSqlServerDbContext<ExampleDbContext>(connectionName: "sqldb");

In the AppHost, when you used the WithReference method to pass a SQL Server database resource to a consuming client project, Aspire injects several configuration properties that you can use in the consuming project.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called sqldb becomes SQLDB_URI.

Use the GetValue() method to obtain these environment variables in consuming projects:

C# - Obtain configuration properties
string sqlHost = builder.Configuration.GetValue<string>("SQLDB_HOST");
string sqlPort = builder.Configuration.GetValue<string>("SQLDB_PORT");
string sqlJDBCConnectionString = builder.Configuration.GetValue<string>("SQLDB_JDBCCONNECTIONSTRING");

You can now retrieve the ExampleDbContext instance using dependency injection. For example, to retrieve the context from a service:

C# — ExampleService.cs
public class ExampleService(ExampleDbContext context)
{
// Use context...
}

For more information on dependency injection, see .NET dependency injection.