我有當前的情況,我有這個基本背景關系,我從中繼承了其他兩個背景關系
namespace AutoAttendant.Data
{
public class BaseDbContext : DbContext
{
protected BaseDbContext(DbContextOptions<BaseDbContext> options)
: base(options)
{
}
protected BaseDbContext(DbContextOptions options)
: base(options)
{
}
public virtual DbSet<LockStatus> LockResult { get; set; }
}
public partial class AutoAttendantContext : BaseDbContext
{
internal AutoAttendantContext(DbContextOptions options)
: base(options)
{
}
public AutoAttendantContext(DbContextOptions<AutoAttendantContext> options)
: base(options)
{
}
}
public partial class ReadOnlyAutoAttendantContext : BaseDbContext
{
internal ReadOnlyAutoAttendantContext(DbContextOptions options)
: base(options)
{
}
public ReadOnlyAutoAttendantContext(DbContextOptions<ReadOnlyAutoAttendantContext> options)
: base(options)
{
}
}
}
我想在這里使用這個背景關系作為引數
namespace AutoAttendant.API.ConfigurationExtensions
{
public static class ServiceCollectionExtension
{
public static IServiceCollection AddPomeloDataSourceConfiguration(this IServiceCollection services, string connectionString, Version version, **BaseDbContext context**)
{
services.AddDbContextPool<**context**>(options => options.UseMySql(connectionString, new MySqlServerVersion(version), op =>
{
op.EnableRetryOnFailure();
}));
return services;
}
}
}
但我不知道如何正確傳遞它,因為在 中使用了一個型別AddDbContextPool,有什么想法嗎?
當我呼叫 AddPomeloDataSourceConfiguration 時,如何將其與繼承的背景關系一起使用
services.AddPomeloDataSourceConfiguration(Configuration.GetConnectionString("DefaultConnection"), new Version(5, 7), AutoAttendantContext);
services.AddPomeloDataSourceConfiguration(Configuration.GetConnectionString("ReadOnlyConnection"), new Version(5, 7), ReadOnlyAutoAttendantContext);
uj5u.com熱心網友回復:
您可以使您的背景關系型別通用:
public static IServiceCollection AddPomeloDataSourceConfiguration<TContext>(
this IServiceCollection services,
string connectionString,
Version version)
where TContext : BaseDbContext
{
services.AddDbContextPool<TContext>(options => options.UseMySql(connectionString, new MySqlServerVersion(version), op =>
{
op.EnableRetryOnFailure();
}));
return services;
}
uj5u.com熱心網友回復:
您可以將其作為型別引數傳遞:
public static IServiceCollection AddPomeloDataSourceConfiguration<TContext>(this IServiceCollection services, string connectionString, Version version)
{
services.AddDbContextPool<TContext>(options => options.UseMySql(connectionString, new MySqlServerVersion(version), op =>
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408716.html
標籤:
