Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this tutorial, you create an ASP.NET Core app that uses a .NET Aspire Entity Framework Core SQL Server integration to connect to SQL Server to read and write support ticket data. Entity Framework Core is a lightweight, extensible, open source object-relational mapper that enables .NET developers to work with databases using .NET objects. You'll learn how to:
To work with .NET Aspire, you need the following installed locally:
For more information, see .NET Aspire setup and tooling, and .NET Aspire SDK.
Visual Studio creates a new ASP.NET Core solution that is structured to use .NET Aspire. The solution consists of the following projects:
To represent a user submitted support request, add the following SupportTicket
model class at the root of the AspireSQLEFCore project.
using System.ComponentModel.DataAnnotations;
namespace AspireSQLEFCore;
public sealed class SupportTicket
{
public int Id { get; set; }
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public string Description { get; set; } = string.Empty;
}
Add the following TicketDbContext
data context class at the root of the AspireSQLEFCore project. The class inherits System.Data.Entity.DbContext to work with Entity Framework and represent your database.
using Microsoft.EntityFrameworkCore;
using System.Reflection.Metadata;
namespace AspireSQLEFCore;
public class TicketContext(DbContextOptions options) : DbContext(options)
{
public DbSet<SupportTicket> Tickets => Set<SupportTicket>();
}
Add the .NET Aspire Entity Framework Core Sql Server library package to your AspireSQLEFCore project:
dotnet add package Aspire.Microsoft.EntityFrameworkCore.SqlServer
Your AspireSQLEFCore project is now set up to use .NET Aspire integrations. Here's the updated AspireSQLEFCore.csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="9.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AspireSQLEFCore.ServiceDefaults\AspireSQLEFCore.ServiceDefaults.csproj" />
</ItemGroup>
</Project>
In the Program.cs file of the AspireSQLEFCore project, add a call to the AddSqlServerDbContext extension method after the creation of the builder
but before the call to AddServiceDefaults
. For more information, see .NET Aspire service defaults. Provide the name of your connection string as a parameter.
using AspireSQLEFCore;
using AspireSQLEFCore.Components;
var builder = WebApplication.CreateBuilder(args);
builder.AddSqlServerDbContext<TicketContext>("sqldata");
builder.AddServiceDefaults();
// Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
var app = builder.Build();
app.MapDefaultEndpoints();
This method accomplishes the following tasks:
TicketContext
with the DI container for connecting to the containerized Azure SQL Database.While developing locally, you need to create a database inside the SQL Server container. Update the Program.cs file with the following code:
using AspireSQLEFCore;
using AspireSQLEFCore.Components;
var builder = WebApplication.CreateBuilder(args);
builder.AddSqlServerDbContext<TicketContext>("sqldata");
builder.AddServiceDefaults();
// Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
var app = builder.Build();
app.MapDefaultEndpoints();
if (app.Environment.IsDevelopment())
{
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<TicketContext>();
context.Database.EnsureCreated();
}
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days.
// You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
The preceding code:
TicketContext
service from the DI container and calls Database.EnsureCreated()
to create the database if it doesn't already exist.Note
Note that EnsureCreated()
is not suitable for production environments, and it only creates the database as defined in the context. It doesn't apply any migrations. For more information on Entity Framework Core migrations in .NET Aspire, see Apply Entity Framework Core migrations in .NET Aspire.
The app requires a form for the user to be able to submit support ticket information and save the entry to the database.
Use the following Razor markup to create a basic form, replacing the contents of the Home.razor file in the AspireSQLEFCore/Components/Pages directory:
@page "/"
@inject TicketContext context
@using Microsoft.EntityFrameworkCore
<div class="row">
<div class="col-md-6">
<div>
<h1 class="display-4">Request Support</h1>
</div>
<EditForm Model="@Ticket" FormName="Tickets" method="post"
OnValidSubmit="@HandleValidSubmit" class="mb-4">
<DataAnnotationsValidator />
<div class="mb-4">
<label>Issue Title</label>
<InputText class="form-control" @bind-Value="@Ticket.Title" />
<ValidationMessage For="() => Ticket.Title" />
</div>
<div class="mb-4">
<label>Issue Description</label>
<InputText class="form-control" @bind-Value="@Ticket.Description" />
<ValidationMessage For="() => Ticket.Description" />
</div>
<button class="btn btn-primary" type="submit">Submit</button>
<button class="btn btn-danger mx-2" type="reset" @onclick=@ClearForm>Clear</button>
</EditForm>
<table class="table table-striped">
@foreach (var ticket in Tickets)
{
<tr>
<td>@ticket.Id</td>
<td>@ticket.Title</td>
<td>@ticket.Description</td>
</tr>
}
</table>
</div>
</div>
@code {
[SupplyParameterFromForm(FormName = "Tickets")]
private SupportTicket Ticket { get; set; } = new();
private List<SupportTicket> Tickets = [];
private void ClearForm() => Ticket = new();
protected override async Task OnInitializedAsync()
{
Tickets = await context.Tickets.ToListAsync();
}
private async Task HandleValidSubmit()
{
context.Tickets.Add(Ticket);
await context.SaveChangesAsync();
Tickets = await context.Tickets.ToListAsync();
ClearForm();
}
}
For more information about creating forms in Blazor, see ASP.NET Core Blazor forms overview.
The AspireSQLEFCore.AppHost project is the orchestrator for your app. It's responsible for connecting and configuring the different projects and services of your app. The orchestrator should be set as the startup project.
Add the .NET Aspire Hosting Sql Server NuGet package to your AspireStorage.AppHost project:
dotnet add package Aspire.Hosting.SqlServer
Replace the contents of the Program.cs file in the AspireSQLEFCore.AppHost project with the following code:
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql")
.AddDatabase("sqldata");
builder.AddProject<Projects.AspireSQLEFCore>("aspiresql")
.WithReference(sql)
.WaitFor(sql);
builder.Build().Run();
The preceding code adds a SQL Server Container resource to your app and configures a connection to a database called sqldata
. The Entity Framework classes you configured earlier will automatically use this connection when migrating and connecting to the database.
The sample app is now ready for testing. Verify that the submitted form data is persisted to the database by completing the following steps:
Select the run button at the top of Visual Studio (or F5) to launch your .NET Aspire project dashboard in the browser.
On the projects page, in the AspireSQLEFCore row, click the link in the Endpoints column to open the UI of your app.
Enter sample data into the Title
and Description
form fields.
Select the Submit button, and the form submits the support ticket for processing — (then select Clear to clear the form).
The data you submitted displays in the table at the bottom of the page when the page reloads.
.NET Aspire feedback
.NET Aspire is an open source project. Select a link to provide feedback:
Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Use databases in a .NET Aspire project - Training
Learn about the database systems that .NET Aspire can connect to using built-in integrations. Then see how to configure connections to, and store data in, relational and nonrelational databases.
Certification
Microsoft Certified: Azure Developer Associate - Certifications
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.
Documentation
.NET Aspire PostgreSQL Entity Framework Core integration - .NET Aspire
Learn how to integrate PostgreSQL with .NET Aspire applications using Entity Framework Core, using both hosting and client integrations.
.NET Aspire Oracle Entity Framework Core integration - .NET Aspire
Learn how to use the .NET Aspire Oracle Entity Framework Core integration, which includes both hosting and client integrations.
.NET Aspire Pomelo MySQL Entity Framework Core integration - .NET Aspire
Learn how to use the .NET Aspire MySQL Entity Framework integration, which includes both hosting and client integrations.