51

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?

Majid
  • 13,853
  • 15
  • 77
  • 113
Tim Coulter
  • 8,705
  • 11
  • 64
  • 95

2 Answers2

68

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.

dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
  • Thanks for your answer - it works. I can't imagine what I did wrong first time around, as I recall implementing exactly the same pattern myself. In any case, problem solved. Thanks. – Tim Coulter Aug 02 '12 at 20:34
  • 3
    Where did you put this class? – Murilo Lima May 28 '14 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. – dougajmcdonald May 28 '14 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. – Ray Suelzer Dec 01 '14 at 23:23
  • @dougajmcdonald - Any reason, why we cannot inject the context into the ContextFactory and return that instead of new'ing a DbContext instance in the create method of the ContextFactory class? – Shiva Naru Apr 09 '15 at 19:05
  • 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 Apr 13 '16 at 05:05
  • @Legolas21 I know this is old, but just FYI, I've found that EF will automatically detect any classes inheriting the interface `IDbContextFactory<>`, according to [this doc](https://msdn.microsoft.com/en-us/library/hh506876(v=vs.113).aspx). I believe this is still true for .NET Core. – Alisson Reinaldo Silva Aug 14 '18 at 21:09
  • 5
    FYI [IDbContextFactory](https://learn.microsoft.com/en-gb/dotnet/api/microsoft.entityframeworkcore.infrastructure.idbcontextfactory-1?view=efcore-2.1) is now obsolete. use [IDesignTimeDbContextFactory](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.design.idesigntimedbcontextfactory-1?view=efcore-2.1) instead. – Søren May 29 '19 at 10:35
1

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.

Julio Nobre
  • 4,196
  • 3
  • 46
  • 49
  • It's better to mark an answer as duplicate than just linking to it. – Gert Arnold Jul 15 '21 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 ? – Julio Nobre Jul 15 '21 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. – Gert Arnold Jul 16 '21 at 06: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 – Julio Nobre Jul 17 '21 at 09:12
  • 1
    IDbContextFactory is no longer obsolete. It was reintroduced in EF Core 5. https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/#using-a-dbcontext-factory-eg-for-blazor – PaulWaldman Dec 08 '21 at 07:06