53

I am trying to use Entity Framework data migrations, as described in this post.

However, when I try to execute the Enable-Migrations step, I receive the following error in Package Manager Console:

The target context 'MyDataContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory

So, I created a factory class that implements IDbContextFactory in the project that contains my DbContext class, but data migrations doesn't appear to recognize it.

Is there something that I should explicitly do to instruct data migrations to use this factory class?

2 Answers 2

69

I also hit this problem as i wrote my context to take a connection string name (and then used ninject to provide it).

The process you've gone through seems correct, here is a snippet of my class implementation if it's of any help:

public class MigrationsContextFactory : IDbContextFactory<MyContext>
{
    public MyContext Create()
    {
        return new MyDBContext("connectionStringName");
    }
}

That should be all you need.

8
  • 3
    Where did you put this class? Commented May 28, 2014 at 14:02
  • 1
    I would suggest putting it in an assembly which contains your 'MyContext' class. I would typically keep such a class in a separate assembly such as MyApp.Data.Model or MyApp.Domain.Model, something along those lines. Commented May 28, 2014 at 19:03
  • 9
    Could you please provide the Ninject configuration code? I don't understand where I would inject the connectionStringName as Create does not take a constructor parameter. Commented Dec 1, 2014 at 23:23
  • 10
    How does EF know about the presence of the factory class - MigrationsContextFactory. Don't we have to register it somewhere so that the framework would know? Is having this class lie independently self-sufficient ? Tim also seems to have acknowledged that it won't be required!
    – Legolas21
    Commented Apr 13, 2016 at 5:05
  • 6
    FYI IDbContextFactory is now obsolete. use IDesignTimeDbContextFactory instead.
    – Søren
    Commented May 29, 2019 at 10:35
2

Like @Soren pointed out, instead of using IDbContextFactory, not supported on some earlier EF Core releases (i.e. EF Core 2.1), we can implement IDesignTimeDbContextFactory<TContext>, which supports the missing ConnectionString parameter.

For a settings.json based aproach, which you can use with either of the referred interfaces, check @Arayn's sample which allows us to define "ConnectionStrings:DefaultConnection" value path

Update 1

According to @PaulWaldman's comment, on EF Core 5 support for IDbContextFactory was reintroduced. For further details, check his comment below.

5
  • It's better to mark an answer as duplicate than just linking to it. Commented Jul 15, 2021 at 17:39
  • Hi @GertArnold. I am not sure what you meant. It seems that you are suggesting that I did something wrong (which I may), but I can't figure it out. Can you elaborate ? Commented Jul 15, 2021 at 22:59
  • 1
    You link to an existing SO answer. It's usually better to (flag to) close the current question as duplicate of the answer you link to. Commented Jul 16, 2021 at 6:12
  • Actually, I've never flagged one, but I will take your advice from now on. However, in this particular question, I will not, since either the question targets a different EF interface. Anyway, thanks for poiting it out Commented Jul 17, 2021 at 9:12
  • 2
    IDbContextFactory is no longer obsolete. It was reintroduced in EF Core 5. learn.microsoft.com/en-us/ef/core/dbcontext-configuration/… Commented Dec 8, 2021 at 7:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.