7

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:

 

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

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

 

Code

The full example is available on GitHub.

Share now!
Ibrahim Šuta
 

  • Scott Sauber

    Thanks for this! This was really helpful.

    • Great! I am glad it’s helpful to people.

  • ismail kahraman

    what about if we have more than one dbcontext? Is there an easy way to add them with writing just one class implementation

  • Ather Ali

    Using EF Core 2.0 i am getting an Error ConfigurationBuilder does not contain a definition of SetBasePath( )

    • Jason Nickle

      Nuget: Microsoft.Extensions.Configuration.Json