Step-by-step guide on how to use the .Net Aspire MSSQL component in Visual Studio. Introduction .Net Aspire framework is used to develop cloud and production-ready distributed applications. It consists of components to handle cloud-native concerns, such as Redis, Postgres, etc. Prerequisites Install .Net 8 Install Visual Studio 2022 version 17 or higher .Net Aspire Workload Container runtime such as Docker Desktop 10 Day .Net Aspire Challenge Edit descriptionsinghsukhpinder.medium.com Objectives Learn how to create a starter project using .Net Aspire with MSSQL EF Core component. Github Sample: The solution structure is divided into the following projects DotnetAspireChallenge.ApiService DotnetAspireChallenge.AppHost DotnetAspireChallenge.ServiceDefaults DotnetAspireChallenge.Web Getting Started Step 1: Install the following NuGet package Install the following Nuget package into the subsequent project “DotnetAspireChallenge.AppHost” dotnet add package Aspire.Hosting.SqlServer In the above project, register a SQL server database and costume the SQL connection using following code. var sql = builder.AddSqlServer("sql") .AddDatabase("sqldata"); var apiService = builder.AddProject<Projects.DotnetAspireChallenge_ApiService>("apiservice") .WithReference(sql); Step 2: Install another NuGet package Install the following Nuget package into the subsequent project “DotnetAspireChallenge.ApiService” dotnet add package Aspire.Microsoft.EntityFrameworkCore.SqlServer then register the context into the Program.cs file as follows builder.AddSqlServerDbContext<MssqlDbContext>("sqldata"); Step 3: Create a “Customer” class public class Customer { public int Id { get; set; } [Required] public string Title { get; set; } = string.Empty; [Required] public string Description { get; set; } = string.Empty; } Step 4: Create an extension class Create an extension class and register a minimal API get method to demonstrate the MSSQL context usage in the API Service public static class AspireMssqlExtension { public static void MapMssqlAspireEndpoint(this WebApplication app) { app.MapGet("/mssql", async (MssqlDbContext mssqlDbContext) => { await mssqlDbContext.Customers.AddAsync(new Customer() { Title = "test@gmail.com", Description = "sukh" }); int rows = await mssqlDbContext.SaveChangesAsync(); if (rows > 0) { return await mssqlDbContext.Customers.FirstOrDefaultAsync(); } else { return null; } }); } } internal class MssqlDbContext(DbContextOptions options) : DbContext(options) { public DbSet<Customer> Customers => Set<Customer>(); } and finally, register in the Program.cs file app.MapMssqlAspireEndpoint(); Step 5: Hit the GET endpoint Finally, navigate to the GET URL shown below in your browser. It will insert the specified customer into the MSSQL database, retrieve the most recently inserted row, and display it as a response. Add additional connection string properties using the JSON syntax { "Aspire": { "Microsoft": { "EntityFrameworkCore": { "SqlServer": { "ConnectionString": "YOUR_CONNECTIONSTRING", "DbContextPooling": true, "DisableHealthChecks": true, "DisableTracing": true, "DisableMetrics": false, "AnotherDbContext": { "ConnectionString": "AnotherDbContext_CONNECTIONSTRING", "DisableTracing": false } } } } } } Congratulations..!! You’ve successfully integrated the MSSQL component into the .Net Aspire project. Github Project GitHub — ssukhpinder/DotnetAspireChallenge: 10 Day .Net Aspire Challenge Step-by-step guide on how to use the .Net Aspire MSSQL component in Visual Studio. Introduction .Net Aspire framework is used to develop cloud and production-ready distributed applications. It consists of components to handle cloud-native concerns, such as Redis, Postgres, etc. Prerequisites Install .Net 8 Install Visual Studio 2022 version 17 or higher .Net Aspire Workload Container runtime such as Docker Desktop 10 Day .Net Aspire Challenge Edit descriptionsinghsukhpinder.medium.com Install .Net 8 Install .Net 8 .Net 8 Install Visual Studio 2022 version 17 or higher Install Visual Studio 2022 version 17 or higher Visual Studio 2022 .Net Aspire Workload .Net Aspire Workload Container runtime such as Docker Desktop 10 Day .Net Aspire Challenge Edit descriptionsinghsukhpinder.medium.com Container runtime such as Docker Desktop Docker Desktop 10 Day .Net Aspire Challenge Edit description singhsukhpinder.medium.com 10 Day .Net Aspire Challenge 10 Day .Net Aspire Challenge Edit description Edit description singhsukhpinder.medium.com Objectives Learn how to create a starter project using .Net Aspire with MSSQL EF Core component. Github Sample : The solution structure is divided into the following projects Github Sample DotnetAspireChallenge.ApiService DotnetAspireChallenge.AppHost DotnetAspireChallenge.ServiceDefaults DotnetAspireChallenge.Web DotnetAspireChallenge.ApiService DotnetAspireChallenge.AppHost DotnetAspireChallenge.ServiceDefaults DotnetAspireChallenge.Web DotnetAspireChallenge.Web Getting Started Step 1: Install the following NuGet package Install the following Nuget package into the subsequent project “ DotnetAspireChallenge.AppHost ” DotnetAspireChallenge.AppHost dotnet add package Aspire.Hosting.SqlServer dotnet add package Aspire.Hosting.SqlServer In the above project, register a SQL server database and costume the SQL connection using following code. var sql = builder.AddSqlServer("sql") .AddDatabase("sqldata"); var apiService = builder.AddProject<Projects.DotnetAspireChallenge_ApiService>("apiservice") .WithReference(sql); var sql = builder.AddSqlServer("sql") .AddDatabase("sqldata"); var apiService = builder.AddProject<Projects.DotnetAspireChallenge_ApiService>("apiservice") .WithReference(sql); Step 2: Install another NuGet package Install the following Nuget package into the subsequent project “ DotnetAspireChallenge.ApiService ” DotnetAspireChallenge.ApiService dotnet add package Aspire.Microsoft.EntityFrameworkCore.SqlServer dotnet add package Aspire.Microsoft.EntityFrameworkCore.SqlServer then register the context into the Program.cs file as follows builder.AddSqlServerDbContext<MssqlDbContext>("sqldata"); builder.AddSqlServerDbContext<MssqlDbContext>("sqldata"); Step 3: Create a “Customer” class public class Customer { public int Id { get; set; } [Required] public string Title { get; set; } = string.Empty; [Required] public string Description { get; set; } = string.Empty; } public class Customer { public int Id { get; set; } [Required] public string Title { get; set; } = string.Empty; [Required] public string Description { get; set; } = string.Empty; } Step 4: Create an extension class Create an extension class and register a minimal API get method to demonstrate the MSSQL context usage in the API Service public static class AspireMssqlExtension { public static void MapMssqlAspireEndpoint(this WebApplication app) { app.MapGet("/mssql", async (MssqlDbContext mssqlDbContext) => { await mssqlDbContext.Customers.AddAsync(new Customer() { Title = "test@gmail.com", Description = "sukh" }); int rows = await mssqlDbContext.SaveChangesAsync(); if (rows > 0) { return await mssqlDbContext.Customers.FirstOrDefaultAsync(); } else { return null; } }); } } internal class MssqlDbContext(DbContextOptions options) : DbContext(options) { public DbSet<Customer> Customers => Set<Customer>(); } public static class AspireMssqlExtension { public static void MapMssqlAspireEndpoint(this WebApplication app) { app.MapGet("/mssql", async (MssqlDbContext mssqlDbContext) => { await mssqlDbContext.Customers.AddAsync(new Customer() { Title = "test@gmail.com", Description = "sukh" }); int rows = await mssqlDbContext.SaveChangesAsync(); if (rows > 0) { return await mssqlDbContext.Customers.FirstOrDefaultAsync(); } else { return null; } }); } } internal class MssqlDbContext(DbContextOptions options) : DbContext(options) { public DbSet<Customer> Customers => Set<Customer>(); } and finally, register in the Program.cs file app.MapMssqlAspireEndpoint(); app.MapMssqlAspireEndpoint(); Step 5: Hit the GET endpoint Finally, navigate to the GET URL shown below in your browser. It will insert the specified customer into the MSSQL database, retrieve the most recently inserted row, and display it as a response. Add additional connection string properties using the JSON syntax { "Aspire": { "Microsoft": { "EntityFrameworkCore": { "SqlServer": { "ConnectionString": "YOUR_CONNECTIONSTRING", "DbContextPooling": true, "DisableHealthChecks": true, "DisableTracing": true, "DisableMetrics": false, "AnotherDbContext": { "ConnectionString": "AnotherDbContext_CONNECTIONSTRING", "DisableTracing": false } } } } } } { "Aspire": { "Microsoft": { "EntityFrameworkCore": { "SqlServer": { "ConnectionString": "YOUR_CONNECTIONSTRING", "DbContextPooling": true, "DisableHealthChecks": true, "DisableTracing": true, "DisableMetrics": false, "AnotherDbContext": { "ConnectionString": "AnotherDbContext_CONNECTIONSTRING", "DisableTracing": false } } } } } } Congratulations..!! You’ve successfully integrated the MSSQL component into the .Net Aspire project. Github Project GitHub — ssukhpinder/DotnetAspireChallenge: 10 Day .Net Aspire Challenge GitHub — ssukhpinder/DotnetAspireChallenge: 10 Day .Net Aspire Challenge GitHub — ssukhpinder/DotnetAspireChallenge: 10 Day .Net Aspire Challenge