How to implement IDbContextFactory for use with Entity Framework data migrations

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

However, when I try to complete the Enable-Migrations step, I get the following error in the 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 a project that contains my DbContext class, but data migration does not seem to recognize it.

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

+42
entity-framework data-migration
Jul 09 2018-12-12T00:
source share
1 answer

I also got into this problem since I wrote my context to take the name of the connection string (and then use ninject to provide it).

The process you went through seems to be correct, here is a snippet of my class implementation, if it has any help:

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

That should be all you need.

+57
Jul 31 2018-12-12T00:
source share



All Articles