我正在尝试创建一个基本的DbContext以便在通用存储库中使用。
目标是要在微服务环境中使用集中式基础存储库和服务,但是每个服务必须传递所确定的dbContext的类型以用于查询。
已经尝试将TContext类型从ApiDbContext转换为纯DbContext,但没有成功。
API在带有EFCore 2.2.6的.NET Core 2.2上运行
代码如下
存储库实现
public class Repository<TContext> : IDisposable, IRepository<TContext>
where TContext : ApiDbContext, new()
存储库接口
public interface IRepository<TContext> : IDisposable where TContext : ApiDbContext
ApiDbContext
public class ApiDbContext : DbContext
{
private static readonly bool[] _migrated = { false };
private readonly IHttpContextAccessor _httpContextAccessor;
public ApiDbContext()
{
}
public ApiDbContext(DbContextOptions<ApiDbContext> options, IHttpContextAccessor httpContextAccessor)
: base(options)
{
_httpContextAccessor = httpContextAccessor;
Database.SetCommandTimeout(300);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
new AuditConfiguration(modelBuilder.Entity<Audit>());
foreach (IMutableForeignKey relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelBuilder);
}
protected static DbContextOptions<T> ChangeOptionsType<T>(DbContextOptions options) where T : DbContext
{
var sqlExt = options.Extensions.FirstOrDefault(e => e is SqlServerOptionsExtension);
if (sqlExt == null)
throw (new Exception("Failed to retrieve SQL connection string for base Context"));
return new DbContextOptionsBuilder<T>()
.UseSqlServer(((SqlServerOptionsExtension)sqlExt).ConnectionString)
.Options;
}
}
AuthDbContext
public class AuthDbContext : ApiDbContext
{
public AuthDbContext(DbContextOptions<AuthDbContext> options, IHttpContextAccessor httpContextAccessor)
: base(ChangeOptionsType<ApiDbContext>(options), httpContextAccessor)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
new UserConfiguration(modelBuilder.Entity<User>());
base.OnModelCreating(modelBuilder);
}
}
例外
TypeLoadException:GenericArguments [0],“ Company.Project.Common._Bases.Infrastructure.Repository” 1 [TContext]上的“ Company.Project.Auth.Infrastructure.Data.AuthDbContext”违反了类型参数“ TContext”。
引发异常的代码(在存储库注入时)
public class Service<T, TContext> : IService<T>
where T : BaseEntity
where TContext : ApiDbContext
{
protected IRepository<TContext> _repository { get; set; }
public Service(IServiceProvider provider)
{
_repository = provider.GetService(typeof(IRepository<TContext>)) as IRepository<TContext>;
}
}
DependencyInjection
public static void Resolve<T>(IServiceCollection services, IConfiguration Configuration) where T : DbContext
{
services.AddDbContext<T>(options =>
options.UseSqlServer(Configuration.GetConnectionString(Configuration["StartupConfig:DefaultConnectionString"]),
b => b.MigrationsAssembly(Configuration["StartupConfig:MigrationAssembly"])
.UseRowNumberForPaging()));
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient(typeof(IService<>), typeof(Service<,>));
services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
}
答案 0 :(得分:0)
尝试添加
.AddTransient<DbContext, ApiDbContext>();
或
.AddTransient<ApiDbContext>();