Skip to content

MigrationsExtensionsDbContextDesignTimeFactory Class

Dennis C. Mitchell edited this page Mar 17, 2019 · 6 revisions

EDennis.AspNetCore.Base provides an abstract implementation of IDesignTimeDbContextFactory in order to simplify/standardize migration-specific configuration aspects of your DbContext (e.g., connection string) and to allow your DbContext class to have just one constructor that takes DbContextOptions<YourDbContextClass> as a parameter.

The base class, MigrationsExtensionsDbContextDesignTimeFactory, makes two assumptions:

  1. Your DbContext class has a single constructor that takes DbContextOptions<YourDbContextClass> as a parameter.
  2. You have configured your connection string in either appsettings.json or appsettings.Development.json with the following key: ConnectionStrings:YourDbContextClass (where YourDbContextClass is the name of your DbContext class).

When these assumptions hold, you can create a trivial subclass of MigrationsExtensionsDbContextDesignTimeFactory as such:

public class YourDbContextClassDesignTimeFactory :
    MigrationsExtensionsDbContextDesignTimeFactory<YourDbContextClass> { }

For convenience, you may wish to include the above class in the file that contains your DbContext class.

Clone this wiki locally