如何实现IDbContextFactory用于实体框架数据迁移 [英] How to implement IDbContextFactory for use with Entity Framework data migrations
问题描述
我正在尝试使用Entity Framework数据迁移,如这篇文章。
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
所以,我创建了一个实现<$包含我的 DbContext
类的项目中的c $ c> 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?
推荐答案
我也碰到这个问题,因为我写了我的上下文来连接字符串名称(然后使用ninject来提供它)。
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");
}
}
这应该是你需要的。
这篇关于如何实现IDbContextFactory用于实体框架数据迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!