带有使用自定义 DbConnection 的 DbContext 的 AddPooledDbContextFactory:服务未注册
AddPooledDbContextFactory with DbContext that uses custom DbConnection: service not registered
我有一个自定义 DbContext SnowflakeDbContext,我需要使用 SnowflakeDbConnection 对其进行初始化才能正常工作:
public class SnowflakeDbContext : DbContext
{
private readonly string connectionString = "";
public SnowflakeDbContext(DbContextOptions<SnowflakeDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
var dbConnection = new SnowflakeDbConnection()
{
ConnectionString = this.connectionString
};
optionsBuilder.UseSqlServer(dbConnection);
optionsBuilder.AddInterceptors(new SnowflakeCommandInterceptor());
}
public DbSet<Opportunity> Opportunities { get; set; } = default!;
public DbSet<Account> Accounts { get; set; } = default!;
}
这适用于 EF Core 5,在 Startup.cs 中(我正在使用 ASP.NET Core 5 Web 应用程序)我使用
.AddDbContext<SnowflakeDbContext>(ServiceLifetime.Singleton)
我想将 SnowflakeDbContext 与 HotChocolate where it is recommended that I use AddPooledDbContextFactory<> in order to support pooling of connections and allowing the system to make simultaneous calls (described here).
一起使用
我修改了Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services
.AddPooledDbContextFactory<SnowflakeDbContext>(options =>
{
var dbConnection = new SnowflakeDbConnection()
{
ConnectionString = this.connectionString
};
options.UseSqlServer(dbConnection);
options.AddInterceptors(new SnowflakeCommandInterceptor());
})
.AddGraphQLServer()
.AddQueryType<Query>();
}
使用以下 GraphQL 查询(使用并行查询):
query GetAccountsInParallel {
a: accounts {
id, name
}
b: accounts {
id, name
}
c: accounts {
id, name
}
}
我收到以下错误:
"No service for type 'SnowflakeGraphQL.Snowflake.SnowflakeDbContext' has been registered.",
我可以加
.AddDbContext<SnowflakeDbContext>()
在 Startup.cs 调用 .AddPooledDbContextFactory<> 之后。现在我得到一个不同的错误:
"A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext."
我在网上看到的所有示例都使用 .UseSqlServer(connectionString),因为我需要使用 .UseSqlServer(dbConnection) 版本,以便能够访问我们的 Snowflake 数据库。
如何在 Startup.cs 中配置我的应用程序以使用 .AddPooledDbContextFactory()?
更新: 从 graphql-workshop 代码开始,先用 SqlServer 替换 Sqlite,然后用我的 SnowflakeDbContext 替换 SqlServer 我让它工作,所以某处一定有细微差别如上所述,在我的代码中导致我的案例失败。
检索账户记录时,我们需要使用[ScopedService]而不是像这样的[Service]:
[UseApplicationDbContext]
public async Task<List<Account>> GetAccounts([ScopedService] SnowflakeDbContext context) => await context.Accounts.ToListAsync();
我有一个自定义 DbContext SnowflakeDbContext,我需要使用 SnowflakeDbConnection 对其进行初始化才能正常工作:
public class SnowflakeDbContext : DbContext
{
private readonly string connectionString = "";
public SnowflakeDbContext(DbContextOptions<SnowflakeDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
var dbConnection = new SnowflakeDbConnection()
{
ConnectionString = this.connectionString
};
optionsBuilder.UseSqlServer(dbConnection);
optionsBuilder.AddInterceptors(new SnowflakeCommandInterceptor());
}
public DbSet<Opportunity> Opportunities { get; set; } = default!;
public DbSet<Account> Accounts { get; set; } = default!;
}
这适用于 EF Core 5,在 Startup.cs 中(我正在使用 ASP.NET Core 5 Web 应用程序)我使用
.AddDbContext<SnowflakeDbContext>(ServiceLifetime.Singleton)
我想将 SnowflakeDbContext 与 HotChocolate where it is recommended that I use AddPooledDbContextFactory<> in order to support pooling of connections and allowing the system to make simultaneous calls (described here).
一起使用我修改了Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services
.AddPooledDbContextFactory<SnowflakeDbContext>(options =>
{
var dbConnection = new SnowflakeDbConnection()
{
ConnectionString = this.connectionString
};
options.UseSqlServer(dbConnection);
options.AddInterceptors(new SnowflakeCommandInterceptor());
})
.AddGraphQLServer()
.AddQueryType<Query>();
}
使用以下 GraphQL 查询(使用并行查询):
query GetAccountsInParallel {
a: accounts {
id, name
}
b: accounts {
id, name
}
c: accounts {
id, name
}
}
我收到以下错误:
"No service for type 'SnowflakeGraphQL.Snowflake.SnowflakeDbContext' has been registered.",
我可以加
.AddDbContext<SnowflakeDbContext>()
在 Startup.cs 调用 .AddPooledDbContextFactory<> 之后。现在我得到一个不同的错误:
"A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext."
我在网上看到的所有示例都使用 .UseSqlServer(connectionString),因为我需要使用 .UseSqlServer(dbConnection) 版本,以便能够访问我们的 Snowflake 数据库。
如何在 Startup.cs 中配置我的应用程序以使用 .AddPooledDbContextFactory()?
更新: 从 graphql-workshop 代码开始,先用 SqlServer 替换 Sqlite,然后用我的 SnowflakeDbContext 替换 SqlServer 我让它工作,所以某处一定有细微差别如上所述,在我的代码中导致我的案例失败。
检索账户记录时,我们需要使用[ScopedService]而不是像这样的[Service]:
[UseApplicationDbContext]
public async Task<List<Account>> GetAccounts([ScopedService] SnowflakeDbContext context) => await context.Accounts.ToListAsync();