EntityFramework Core – Add an implementation of IDesignTimeDbContextFactory

Problem

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.

 

Solution

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() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); var builder = new DbContextOptionsBuilder<CodingBlastDbContext>(); var connectionString = configuration.GetConnectionString("DefaultConnection"); builder.UseSqlServer(connectionString); 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.

 

Code

The full example is available on GitHub.

Ibrahim Šuta

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