• Home >>
  • ASP.NET Core >>

EntityFramework Core – Add an implementation of IDesignTimeDbContextFactory – Multiple DbContext’s


We already talked about problem when Entity Framework Core tooling requires you to implement IDesignTimeDbContextFactory<DbContext> – check out this post.

It gets interesting when you have more than one DbContext in your application and you want to add another implementation of IDesignTimeDbContextFactory, without repeating the code and making sure that EF Core tooling picks the right one.



We have more than DbContext  in Database project and we need an implementation of IDesignTimeDbContextFactory<DbContext>.

First DbContext:

Second DbContext:

If we try to add migrations from Database project:

We get the following error:

More than one DbContext was found. Specify which one to use. Use the ‘-Context’ parameter for PowerShell commands and the ‘–context’ parameter for dotnet commands.

If we specify the context:

Now we get this message:

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



Previously, for one DbContext and one implementation of IDesignTimeDbContextFactory<DbContext> we had this code:

One of the things we can do is to make this class and method generic and make two classes that will inherit this class.

One way to do generic implementation of DesignTimeDbContextFactory and CreateDbContext:

Now we can have two separate DesignTimeDbContextFactory implementations:

Now we can run the following command:


Ibrahim Šuta

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