我有一个数据访问层(一个 C# 库),一个我打算用作服务的 .NET Core 应用程序和几个需要与该服务对话的项目,包括一个 Blazor WASM 项目。
我的 DAL 有一个如下所示的 DbContext:
public class QualityAssuranceDbContext : DbContext
{
public QualityAssuranceDbContext(DbContextOptions<QualityAssuranceDbContext> options)
: base(options)
{
}
public QualityAssuranceDbContext()
{
}
public DbSet<Aspect> Aspects { get; set; }
//PLUS OTHER ENTITIES REDACTED FOR BREVITY
}
然后在我单独的 .NET Core 项目中,我有一个
appsettings.json
文件,其中包含我在 Program.cs
中引用的连接字符串,如下所示:
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<QualityAssuranceDbContext>(options =>
options.UseSqlServer(connectionString));
我正在使用通用存储库/工作单元模式,所以在我的 .NET Core 项目中我还有一个 UnitOfWork 类,如下所示:
public class UnitOfWork : IUnitOfWork
{
private QualityAssuranceDbContext _context = new QualityAssuranceDbContext();
private IBaseRepository<Aspect> _aspects;
public IBaseRepository<Aspect> Aspects => _aspects ??= new BaseRepository<Aspect>(_context);
public void Dispose()
{
_context.Dispose();
GC.SuppressFinalize(this);
}
public async Task Save(HttpContext httpContext)
{
//DO STUFF
}
}
和一个看起来像这样的 BaseRepository:
public class BaseRepository<T> : IBaseRepository<T> where T : class
{
private readonly QualityAssuranceDbContext _context;
private readonly DbSet<T> _db;
public BaseRepository(QualityAssuranceDbContext context)
{
_context = context;
_db = _context.Set<T>();
}
public async Task<IList<T>> GetAll(
Expression<Func<T, bool>> expression = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
Func<IQueryable<T>, IIncludableQueryable<T, object>> includes = null
)
{
IQueryable<T> query = _db;
if (expression != null)
{
query = query.Where(expression);
}
if (orderBy != null)
{
query = orderBy(query);
}
if (includes != null)
{
query = includes(query);
}
return await query.AsNoTracking().ToListAsync();
}
//PLUS OTHER CRUD ACTIONS
}
然后我像这样在
UnitOfWork
中引用Program.cs
:
builder.Services.AddTransient<IUnitOfWork, UnitOfWork>();
最后,在我的 Blazor WASM 应用程序的服务器项目中,我有一个如下所示的控制器:
public class QualityAssuranceController : ControllerBase
{
private readonly QualityAssuranceService _qualityAssuranceService;
public QualityAssuranceController()
{
this._qualityAssuranceService = new QualityAssuranceService();
}
[HttpGet]
public async Task<IActionResult> GetAspects(bool includeDeleted = false)
{
var claimsPrincipal = User;
var schoolId = claimsPrincipal.FindFirst(c => c.Type == "schoolId");
if (schoolId != null)
{
var aspects = await _qualityAssuranceService.GetAspects(int.Parse(schoolId.Value), false);
var aspectsDto = (from aspect in aspects
select new AspectDto()
{
Name = aspect.Name,
}).ToList();
return Ok(aspectsDto);
}
else
{
return NoContent();
}
}
}
控制器成功调用了我的 QualityAssuranceService,然后它尝试通过我的 UnitOfWork 实例化一个新的 BaseRepository。它似乎失败的一点是:
_db = _context.Set<T>();
我在开发人员工具中检查网络选项卡时收到的错误消息是:
System.InvalidOperationException:没有为此 DbContext 配置数据库提供程序。可以通过覆盖“DbContext.OnConfiguring”方法或在应用程序服务提供者上使用“AddDbContext”来配置提供者。如果使用“AddDbContext”,则还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象,并将其传递给 DbContext 的基本构造函数。
我哪里错了?