You don’t need a IDesignTimeDbContextFactory

If you have ever gotten the error message, that tells you to add IDesignTimeDbContextFactory to your project, when doing EF Core migrations.

This particular one:

Unable to create an object of type ‘MyContext’. Add an implementation of ‘IDesignTimeDbContextFactory’ to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

You will probably be scratching your head, asking some questions and maybe even swear a little. Because the console will tell you to implemnt a IDesignTimeDbContextFactory so that is

But don’t fret, you don’t need one. You can avoid it by having making sure of the following:

  • The entire solution must be buildable and be able to run!
    • If you have a project that cannot build, you can exclude it with the build configuration manager.
  • Default constructor
    • For the system to be able to build migrations from your context, it needs to be able to instantiate it with a default constructor!
  • Implement the correct overloaded constructor!
    • DbContextOptions<TContext> instead of DbContextOptions.

ASP.NET Core

If you are running an ASP.NET Core application, and you use the ServiceProvider (IoC) that is built in, then you must make sure that your DbContext and its’ dependencies are registered.

You must also make sure that you are using the DbContextOptions<TContext> overloaded constructor and not the
DbContextOptions
overloaded constructor.

 public class MyContext : DbContext
 {
      public MyContext(DbContextOptions<MyContext> options) : base(options)
      {
      }

      public MyContext()
      {
      }
}

When do I need a IDesignTimeDbContextFactory?

When your DbContext exists in another project, like a shared library. Meaning that is has no context to run from. EF Core migrations works by actually running your code, if that is not possible, then you need to provide information about how your DbContext can be created, which you do with this factory:

public class MyContextContextFactory : IDesignTimeDbContextFactory<MyContext>
{
  public MyContextCreateDbContext(string[] args)
  {
      IConfigurationRoot configuration = new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("appsettings.json")
          .Build();

      // Here we create the DbContextOptionsBuilder manually.        
      var builder = new DbContextOptionsBuilder<MyContext>();
        
      // Build connection string. This requires that you have a connectionstring in the appsettings.json
      var connectionString = configuration.GetConnectionString("DefaultConnection");
      builder.UseSqlServer(connectionString);
      // Create our DbContext.
      return new MyContext(builder.Options);
  }
}

And now you can run your migration from the package management console:

Add-Migration InitialCreate -Context MyContext -Project net.snede.MyProject -Verbose

Or from command line:

dotnet ef migrations add InitialCreate --context MyContext --project net.snede.MyProject

Hope this saves you some time!

// André Snede Kock.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *