Get started with the SQL Server Entity Framework Core integrations
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
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.
Set up hosting integration
Section titled “Set up hosting integration”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 add sqlserverDie Aspire CLI ist interaktiv; das passende Suchergebnis wählen, wenn gefragt:
Select an integration to add:
> sqlserver (Aspire.Hosting.SqlServer)> Other results listed as selectable options...#:package Aspire.Hosting.SqlServer@*<PackageReference Include="Aspire.Hosting.SqlServer" Version="*" />Next, in the AppHost project, create instances of SQL Server server and database resources, then pass the database to the consuming client projects:
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();Use the integration in client projects
Section titled “Use the integration in client projects”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.
Set up client projects
Section titled “Set up client projects”In each of these consuming client projects, install the Aspire SQL Server EF Core client integration:
dotnet add package Aspire.Microsoft.EntityFrameworkCore.SqlServer#:package Aspire.Microsoft.EntityFrameworkCore.SqlServer@*<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="*" />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.
builder.AddSqlServerDbContext<ExampleDbContext>(connectionName: "sqldb");Use injected SQL Server properties
Section titled “Use injected SQL Server properties”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:
string sqlHost = builder.Configuration.GetValue<string>("SQLDB_HOST");string sqlPort = builder.Configuration.GetValue<string>("SQLDB_PORT");string sqlJDBCConnectionString = builder.Configuration.GetValue<string>("SQLDB_JDBCCONNECTIONSTRING");Use SQL Server resources in client code
Section titled “Use SQL Server resources in client code”You can now retrieve the ExampleDbContext instance using dependency injection. For example, to retrieve the context from a service:
public class ExampleService(ExampleDbContext context){ // Use context...}For more information on dependency injection, see .NET dependency injection.