在同一个项目中同时使用 AddDbContextFactory() 和 AddDbContext() 扩展方法 [英] Use both AddDbContextFactory() and AddDbContext() extension methods in the same project

查看:63
本文介绍了在同一个项目中同时使用 AddDbContextFactory() 和 AddDbContext() 扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 EF Core 文档的 DbContext 配置部分.

I'm trying to use the new DbContextFactory pattern discussed in the DbContext configuration section of the EF Core docs.

我已经在我的 Blazor 应用程序中成功启动并运行了 DbContextFactory,但我想保留直接注入 DbContext 实例的选项,以保持我的现有代码有效.

I've got the DbContextFactory up and running successfully in my Blazor app, but I want to retain the option to inject instances of DbContext directly in order to keep my existing code working.

但是,当我尝试这样做时,我遇到了以下错误:

However, when I try to do that, I'm getting an error along the lines of:

System.AggregateException:某些服务无法构造(验证服务描述符时出错'服务类型:Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext] Lifetime: Singleton ImplementationType: Microsoft.EntityFrameworkCore.Internal.DbContextFactory1[MyContext]':无法使用范围服务来自单例Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext]"的Microsoft.EntityFrameworkCore.DbContextOptions1[MyContext]".)--->System.InvalidOperationException:验证服务时出错描述符 'ServiceType:Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext] Lifetime: Singleton ImplementationType: Microsoft.EntityFrameworkCore.Internal.DbContextFactory1[MyContext]':无法使用范围服务来自单例Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext]"的Microsoft.EntityFrameworkCore.DbContextOptions1[MyContext]".--->System.InvalidOperationException:无法使用范围服务'Microsoft.EntityFrameworkCore.DbContextOptions1[MyContext]' 来自单例 'Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext]'.

System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext] Lifetime: Singleton ImplementationType: Microsoft.EntityFrameworkCore.Internal.DbContextFactory1[MyContext]': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[MyContext]' from singleton 'Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext]'.) ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext] Lifetime: Singleton ImplementationType: Microsoft.EntityFrameworkCore.Internal.DbContextFactory1[MyContext]': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[MyContext]' from singleton 'Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext]'. ---> System.InvalidOperationException: Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[MyContext]' from singleton 'Microsoft.EntityFrameworkCore.IDbContextFactory1[MyContext]'.

我在试验时也设法得到了这个错误:

I also managed to get this error at one point while experimenting:

无法解析范围服务'Microsoft.EntityFrameworkCore.DbContextOptions`1[MyContext]' 来自根提供程序.

Cannot resolve scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions`1[MyContext]' from root provider.

理论上可以同时使用 AddDbContextAddDbContextFactory 吗?

Is it theoretically possible to use both AddDbContext and AddDbContextFactory together?

推荐答案

就是说,这完全是为了了解游戏中各种元素的生命周期并正确设置它们.

It is, it's all about understanding the lifetimes of the various elements in play and getting those set correctly.

默认情况下,由 AddDbContextFactory() 扩展方法创建的 DbContextFactory 具有单例生命周期.如果您使用带有默认设置的 AddDbContext() 扩展方法,它将创建一个 DbContextOptionsScoped 生命周期(见源代码在这里>Singleton 不能使用 Scoped 生命周期较短的东西,会抛出错误.

By default the DbContextFactory created by the AddDbContextFactory() extension method has a Singleton lifespan. If you use the AddDbContext() extension method with it's default settings it will create a DbContextOptions with a Scoped lifespan (see the source-code here), and as a Singleton can't use something with a shorter Scoped lifespan, an error is thrown.

为了解决这个问题,我们需要将 DbContextOptions 的生命周期也更改为Singleton".这可以通过显式设置 AddDbContext()

To get round this, we need to change the lifespan of the DbContextOptions to also be 'Singleton'. This can be done using by explicitly setting the scope of the DbContextOptions parameter of AddDbContext()

services.AddDbContext<FusionContext>(options =>
    options.UseSqlServer(YourSqlConnection),
    contextLifetime: ServiceLifetime.Transient, 
    optionsLifetime: ServiceLifetime.Singleton);

关于这个在 EF 核心 GitHub 存储库上有一个非常好的讨论,从这里开始.还值得一看 DbContextFactory 这里.

There's a really good discussion of this on the EF core GitHub repository starting here. It's also well worth having a look at the source-code for DbContextFactory here.

或者,您还可以通过 在构造函数中设置ServiceLifetime参数:

Alternatively, you can also change the lifetime of the DbContextFactory by setting the ServiceLifetime parameter in the constructor:

services.AddDbContextFactory<FusionContext>(options => 
    options.UseSqlServer(YourSqlConnection), 
    ServiceLifetime.Scoped);

选项应该完全配置就像普通 DbContext 一样 因为这些是将在工厂创建的 DbContext 上设置的选项.

The options should be configured exactly as you would for a normal DbContext as those are the options that will be set on the DbContext the factory creates.

这篇关于在同一个项目中同时使用 AddDbContextFactory() 和 AddDbContext() 扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆