Skip to content

The DbContext

The EFCore Module will automatically register classes extending from DbContext with the container as a scoped dependency. It will also configure them from IConfiguration.

A valid DbContext for the EFCore Module could be:

C#
// Provide a name for obtaining a Connection String.
// Read more on the Connection Strings article.
[DefaultSchemaName("UserProfile")]
internal class UsersCatalogDbContext
    : SuiteDbContext<UsersCatalogDbContext> // Implement SuiteDbContext
{
    public UsersCatalogDbContext(
        // Auto generated constructor that injects required params
        // and pass them to the base class.
        DbContextOptions<UsersCatalogDbContext> contextOptions)
        : base(contextOptions)
    {
    }
}

Some key points here are:

  1. We are providing a name for a Connection String using the [DefaultSchemaName] attribute. This will configure our DbContext to fetch a Connection String with that name from IConfiguration. Read more on Connection Strings]
  2. We are extending from SuiteDbContext<TMyDbContext>. This is not required, but forces you to provide the right options, which are required for the Suite's Configuration to be applied to the DbContext
  3. We are injecting the DbContextOptions<TDbContext> that will contain the configuration for our DbContext generated by the Suite. Read more on Configuring DbContext

Important

We recommend to always extend from SuiteDbContext<MyDbContext>. However, if that's not possible, you can inject DbContextOptions<T> and use the base(options) constructor of the DbContext class in order to integrate with the Suite's Configuration.