EntityFramework Core – Add an implementation of IDesignTimeDbContextFactory


Your DbContext in a separate project – class library project. You are trying to add new migration and update database, and you are running into this error:

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



So you need to implement this interface, and you are not sure how to do it.

You can add a class that implements IDesignTimeDbContextFactory inside of your Web project.

Here is the sample code:

public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<CodingBlastDbContext>
    public CodingBlastDbContext CreateDbContext(string[] args)
        IConfigurationRoot configuration = new ConfigurationBuilder()

        var builder = new DbContextOptionsBuilder<CodingBlastDbContext>();

        var connectionString = configuration.GetConnectionString("DefaultConnection");


        return new CodingBlastDbContext(builder.Options);


Then, navigate to your Database project and run the following from command line:

dotnet ef migrations add InitialMigration -s ../Web/
dotnet ef database update -s ../Web/

-s stands for startup project and ../Web/ is the location of my web/startup project.



The full example is available on GitHub.

Ibrahim Šuta

Software Consultant interested and specialising in ASP.NET Core, C#, JavaScript, Angular, React.js.