我有一个简单的DbContext
看起来像:
public class MyDbContext : DbContext
{
private readonly IUserContext _userContext;
public MyDbContext(IUserContext userContext) : base("DefaultConnectionString")
{
_userContext = userContext;
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, Configuration>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ... Here I need to creates some filters using the IUserContext dependency
base.OnModelCreating(modelBuilder);
}
}
此DbContext
使用Func<T>
工厂,using the guidelines in the Simple Injector documentation连接:container.RegisterFuncFactory<DbContext, MyDbContext>(Lifestyle.Scoped);
public static void RegisterFuncFactory<TService, TImpl>(
this Container container, Lifestyle lifestyle = null)
where TService : class
where TImpl : class, TService
{
lifestyle = lifestyle ?? Lifestyle.Transient;
var producer = lifestyle.CreateProducer<TService, TImpl>(container);
container.RegisterSingleton<Func<TService>>(producer.GetInstance);
}
但显然,由于此消息,DbContext
无法使用这种简单的案例:
目标语境&#39; MyDbContext&#39;是不可构建的。添加默认值 构造函数或提供IDbContextFactory的实现。
我真的不喜欢IDbContextFactory
的想法,所以我能想到的唯一解决方案是删除对MyDbContext
的依赖,将其设置为属性,修改{{1方法并手动初始化上下文:
RegisterFuncFactory
虽然不优雅但它有效,但是还有另一个和更好的&#34;做我需要的方式?我喜欢明确依赖于上下文,但似乎不可能。
更新
错误来自:
&#39; System.Data.Entity.Migrations.Infrastructure.MigrationsException&#39; 发生在EntityFramework.dll中但未在用户代码中处理
在这段代码中,Query方法的return语句在这里:
internal static void RegisterFuncFactory<TService, TImpl>(this Container container, Func<TImpl> instanceProducer, Lifestyle lifestyle = null) where TService : class where TImpl : class, TService
{
lifestyle = lifestyle ?? Lifestyle.Transient;
var producer = lifestyle.CreateProducer<TService>(instanceProducer, container);
container.Register<Func<TService>>(() => producer.GetInstance, Lifestyle.Singleton);
}
container.RegisterFuncFactory<DbContext, MyDbContext>(() => new MyDbContext
{
UserContext = container.GetInstance<IUserContext>()
}, Lifestyle.Scoped);
答案 0 :(得分:2)
添加第二个(默认)构造函数。这样,EF迁移可以在从命令行运行时使用此构造函数,而您可以让应用程序使用第二个构造函数。
当您添加第二个构造函数时,DbContext
上的Simple Injector的自动连接功能会松动,但这不应该是个问题;您可以按如下方式简单地连接上下文:
IUserContext userContext = new AspNetUserContext();
container.RegisterSingleton<IUserContext>(userContext);
var contextProducer = Lifestyle.Scoped.CreateProducer<DbContext>(
() => new MyDbContext(userContext),
container);
container.RegisterSingleton<Func<DbContext>>(contextProducer.GetInstance);
答案 1 :(得分:0)
这个答案只是为了向更多用户显示我最终的结果。 @Steven的回答是正确的答案。
为了能够在支持迁移的同时将依赖项注入DbContext
,我们必须使用两个构造函数。一个用于迁移,一个用于应用程序。
public class MyDbContext : DbContext
{
private readonly IUserContext _userContext;
// For migrations
public MyDbContext() : base("DefaultConnectionString")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, Configuration>());
}
// For applications
public MyDbContext(IUserContext userContext) : base("DefaultConnectionString")
{
_userContext = userContext;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ... Code removed for brevity
base.OnModelCreating(modelBuilder);
}
}
然后将其连接到组合根中,如:
public static void RegisterEntityFramework<TContext>(this Container container, Func<TContext> context) where TContext : DbContext
{
if (container == null) throw new ArgumentNullException(nameof(container));
var contextProducer = Lifestyle.Scoped.CreateProducer<DbContext>(context, container);
container.RegisterSingleton<Func<DbContext>>(() => contextProducer.GetInstance);
}
var userContext = new AspNetHttpUserContext();
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
container.RegisterSingleton<IUserContext>(userContext);
container.RegisterEntityFramework(() => new WayFinderDbContext(userContext));
container.Verify();