How to implement IDbContextFactory for use with Entity Framework data migrations

data-migration entity-framework

Question

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?

1
44
5/6/2015 7:48:36 PM

Accepted Answer

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.

60
7/31/2012 8:21:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow