Overview

3 Using database and storage components

The chapter explains how Aspire can work with a wide range of data storage technologies, from existing external databases to resources that Aspire can host and manage itself. It begins with the simplest case: connecting an app to an already-provisioned database by using a connection string stored in the host application’s configuration. Aspire’s service discovery then makes that connection available to the services that need it, so each service does not have to manage its own database settings.

From there, the chapter moves into SQL-based databases and shows the common patterns that apply across providers. It describes how Aspire can host SQL Server, Oracle, PostgreSQL, and Azure SQL Server, usually by adding a provider-specific host library, creating a database resource, and passing that resource to the consuming service with references and readiness checks. It also compares two ways of consuming SQL data: raw SQL through ADO.NET-style clients and higher-level access through Entity Framework. Across these examples, the chapter emphasizes shared concepts such as persistent container lifetimes for development, automatic database creation in newer Aspire versions, and the use of common entity models and DTOs to separate storage concerns from the UI.

The later sections broaden the discussion to NoSQL and Azure storage services. MongoDB is presented as a document-oriented option that fits product reviews and other flexible, nested data, while Cosmos DB is introduced as a globally distributed Azure service that can also run in emulated mode locally. The chapter then shows how Azure Storage can be used for both Table Storage and Blob Storage: Table Storage for structured metadata with partition and row keys, and Blob Storage for file-like content such as CSV product specifications. Throughout, the chapter’s main message is that Aspire provides a consistent way to host, configure, and consume diverse storage systems while keeping application code clean and portable.

SQL Server component hosted in Aspire
figure
Products returned from the API endpoint
figure

Summary

  • The Aspire ecosystem has integrations for four major SQL-based database types: SQL Server, Oracle, PostgreSQL, and MySQL. Although all these integrations rely on different NuGet libraries for hosting inside Aspire, the setup syntax is similar across these libraries.
  • When hosting libraries are used with client libraries to register database components, Aspire can generate connection strings automatically.
  • All supported database integrations can be managed through Entity Framework, an object-relational mapper that abstracts away SQL interactions in C# code.
  • SQL Server, PostgreSQL, and MySQL integrations have client libraries that allow raw SQL database integrations. Oracle integration works only with Entity Framework.
  • NoSQL doesn’t refer to any specific data storage format. Several database types are classed as NoSQL. Sometimes, NoSQL databases of the same type are set up differently.
  • Aspire natively supports integrations with popular NoSQL databases and Azure-native data storage services.
  • Various Azure services can use emulators, so we don’t have to connect our local development machine to a real Azure instance.

FAQ

How do I connect an Aspire app to an existing database using a connection string?Use builder.AddConnectionString("database") in the host project, and make sure the name matches an entry in the host app’s ConnectionStrings section. Aspire then supplies that connection string to services through service discovery.
Why does Aspire use the connection name instead of hardcoding the database connection string in each service?Using the connection name lets Aspire manage service discovery and inject the correct connection string automatically. This keeps the connection string out of individual service settings and simplifies configuration.
What SQL database types are supported by Aspire in this chapter?The chapter covers SQL Server, Oracle, PostgreSQL, and MySQL as supported SQL-based relational database options.
What is the difference between the host library and the consumer library in an Aspire database integration?The host library is used in the Aspire host project to register, configure, and launch the database resource. The consumer library is used in application services to connect to and work with that database.
When should I use raw SQL instead of Entity Framework with SQL Server in Aspire?Use raw SQL when you want direct control over queries and commands through ADO.NET-style APIs. Use Entity Framework when you prefer an ORM abstraction, LINQ queries, and a higher-level model-based approach.
Why are separate entity models and DTOs recommended?Entity models represent database records, while DTOs are used to transfer only the data needed across service boundaries or to the UI. This reduces payload size, avoids exposing database-specific fields, and keeps the presentation layer independent from storage details.
What does WithLifetime(ContainerLifetime.Persistent) do for a database container?It keeps the database container reusable between runs instead of destroying it each time the app is restarted. This prevents data loss during local development.
How do I register and use SQL Server with Entity Framework in Aspire?Register the DbContext with builder.AddSqlServerDbContext<ProductsDbContext>("sqldb"), then inject the context where needed. You can initialize the database with context.Database.EnsureCreated() and query data using LINQ.
What is the main reason to use MongoDB or other NoSQL databases in this chapter?NoSQL databases are useful when the data shape is flexible, relationships are less important, or the application needs scalable document-style storage. MongoDB is used here for product reviews, which fit well as documents.
How do Azure Table Storage and Blob Storage differ in the examples shown?Table Storage is used for structured NoSQL reference data, such as product metadata entities with PartitionKey and RowKey. Blob Storage is used for file-like data, such as downloadable CSV product specs.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free