Database
A database stores data permanently and in order and lets you query it specifically and change it consistently. Relational databases use tables and SQL, NoSQL databases documents or key-value pairs.
A database stores data permanently and in order and lets you query it specifically and change it safely. Almost every application with user data has one.
Two families
In its guide to cloud-native .NET applications, Microsoft compares two basic types:
- Relational databases store data in tables with a fixed schema, are operated with SQL, and offer ACID guarantees (atomicity, consistency, isolation, durability). Examples: SQL Server, Azure SQL Database, PostgreSQL.
- NoSQL databases often store key-value pairs or JSON documents, scale easily, and typically guarantee ACID only within a partition. An example in Azure: Azure Cosmos DB.
Which type fits depends on the access patterns. Business data with relationships and transactions, such as orders and payments, usually belongs in a relational database. Flexible, very large, or globally distributed data often speaks for NoSQL. Mixing both in one solution (the fitting database per service) is possible.
Databases in .NET
For C# code, you usually take Entity Framework Core, an object-relational mapper. It lets you work with .NET objects instead of handwritten SQL: entity classes and a DbContext form the model, you write queries with LINQ, SaveChangesAsync stores changes, and migrations evolve the schema along with the model. A DbContext is requested through dependency injection.
var contacts = await db.Contacts
.Where(c => c.Company == "Example Inc.")
.OrderBy(c => c.Name)
.ToListAsync();
A mapper saves you typing, but not understanding: whoever does not know the generated SQL, indexes, and execution plans builds slow queries.
An example from the Kladde
The Kladde stores contacts, companies, and appointments in SQL Server, locally in a container and in Azure as an Azure SQL Database. A rule such as "no double appointment" can be secured with constraints in the database, and changes to the schema go through migrations.
Common misconceptions
- A database and a database server are not the same. The server (or service) hosts databases.
- NoSQL is not progress over SQL, but a different trade-off.
- A database is not a backup. Restoring has to be planned and rehearsed.
- Not every amount of data is big data.
- The backend is the application in front of the database, not the database itself.
In the course
Databases are the topic of K2, module 2.4: SQL Server in a container, SQL: querying, Modeling data, and EF Core: DbContext and mapping. Running it in the cloud follows in K3, module 3.6, with Azure SQL.
Further reading on the blog
Sources
- Microsoft Learn, SQL vs. NoSQL data: https://learn.microsoft.com/dotnet/architecture/cloud-native/relational-vs-nosql-data
- Microsoft Learn, Entity Framework Core: https://learn.microsoft.com/ef/core/