如何使用DI

时间:2019-08-22 14:08:49

标签: c# .net-core entity-framework-core

我将Entity Framework Extensions包用于批量插入方法,但是,执行此方法时,我得到添加ContextFactory的异常。因此,根据https://entityframework-extensions.net/context-factory,我可以像EntityFrameworkManager.ContextFactory = context => new CurrentContext();一样添加它。

但是,我通过DI(依赖注入)添加数据库,如下所示:services.AddDbContext<DatabaseContext>(options => { options.UseSqlServer(connection); }, ServiceLifetime.Transient);

所以我的猜测是联系工厂应该通过DI访问内容,但是,我不知道如何做。我尝试过的一件事是:

EntityFrameworkManager.ContextFactory = context =>
{
    var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
    optionsBuilder.UseSqlServer(connection);
    return new DatabaseContext(optionsBuilder.Options);
};

但这给了我The Provider could not be resolved. You must explicitly set the Provider.

例外

关于如何解决此问题的任何线索吗?

另外:如果重要的话,我正在使用.Net core 3(预览版8)。

0 个答案:

没有答案