Entity Framework

Entity Framework Core is what we use throughout the application. In our experience it is by far the superior method to writing database-driven applications. Adding new tables and entities is a breeze and querying the data through Linq is effortless.

The ApplicationDbContext Class

This is the core class used throughout the application whenever database access is needed. As of .Net 5, Microsoft has provided us with a safe way to create a context inside a Blazor page. There is now an IDbContextFactory interface that you can inject into your Blazor pages. To get an actual ApplicationDbContext, simply call CreateDbContext on the factory as such: await using var dbContext = DbContextFactory.CreateDbContext(). Note that a context should not be stored as a property or field in a Blazor page. Create a new context for each function where it is needed. By design it is very cheap to do so and you need not worry about performance. Please be warned that ApplicationDbContextis (by design) not thread safe. This poses a problem in Blazor applications, because they are inherently asynchronous. So when using the context in your code, take great care not to access it concurrently from multiple threads or synchronization contexts. One particularly troublesome scenario we have run across is accessing the context in the various component lifecycle methods, such as OnInitializedAsync. We recommend that if you need to access the database, you do so via an http client (instead of ApplicationDbContext, which we will discuss later. You can safely access the context in event handlers, such as button clicks, because it's not likely that another thread will be accessing it at the same time.

The factory is configured in Startup.InitializeDbContext. Microsoft has made configuring it very simple and all that is needed is the connection string. Start Blazoring uses MS SQL Server for its data store. While you can use a different engine, we recommend sticking to MS SQL Server, especially if you are deploying your application to Azure. If you would like to use a different engine, please know that we do not support that. We can try to help, but we cannot guarantee that it will work.

You will notice that the class has a number of DbSet<EntityName> declarations. These are the representations for the tables in the database. Whenever adding a new table, you have to create a new DbSet for it. Here is how to do that. you must follow all the steps, especially adding a new migration, otherwise it will be more difficult to deploy your app to production.

Adding a New Entity

  1. Add a new entity class in Internal\Data\Entities\. Feel free to also create your own folder structure if you do not want to modify the Internal contents.
public class Entity
    public int Id { get; set; }

  1. Add a public DbSet<Entity> Entities { get; set; } to ApplicationDbContext.
  2. Open the Package Manager Console and run add-migration "AddingEntity".
  3. Run update-database.
  4. Ensure that your tables were created and the columns look as intended. Remember, use attribute descriptors to get the desired table definition. Do not edit the table structure directly or the Migrations classes. For more information refer to Microsoft's Documentation.

Table of Contents

An error has occurred. This application may no longer respond until reloaded. Reload 🗙