繁体   English   中英

C#:从 Net Core 中的 DbContext 继承,构造函数:没有为此 DbContext 配置数据库提供程序

[英]C#: Inherit from DbContext in Net Core, Constructor: No database provider has been configured for this DbContext

我目前有 PropertyApplication DbContext 如下,

public partial class PropertyContext : DbContext
{
    public PropertyContext()
    {
    }

    public PropertyContext(DbContextOptions<PropertyContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Address> Address { get; set; }
    public virtual DbSet<BoundaryChangeEvent> BoundaryChangeEvent { get; set; }

我想从这个 PropertyDbContext 中得到 inheritance。 这在构造函数中是否正确完成? 试图通过下面的单元测试,它会覆盖保存更改以引入审核用户信息。 只是好奇下面的构造函数语句是否正确? 或者我应该尝试使用 AuditablePropertyContext 选项尝试下面的选项 2?

public class AuditablePropertyContext : PropertyContext
{
    private int _user;

    public AuditablePropertyContext()
    {
    }

    public AuditablePropertyContext(DbContextOptions<PropertyContext> options, UserResolverService userService)
        : base(options)
    {
        _user = userService.GetUser();
    }

    public void ApplyCreatedBy()
    {
        var modifiedEntities = ChangeTracker.Entries<ICreatedByUserId>().Where(e => e.State == EntityState.Added);
        foreach (var entity in modifiedEntities)
        {
            entity.Property("CreatedByUserId").CurrentValue = _user;
        }
    }

    public override int SaveChanges()
    {
        ApplyCreatedBy();
        return base.SaveChanges();
    }

}

选项 2:

我在尝试执行此操作时收到错误,

public AuditablePropertyContext(DbContextOptions<AuditablePropertyContext> options, UserResolverService userService)
    : base(options)
{
    _user = userService.GetUser();
}

错误:

错误 CS1503 参数 1:无法从“Microsoft.EntityFrameworkCore.DbContextOptions IPTS.PropertyManagement.Infrastructure.Auditable.Data.AuditablePropertyContext”转换为“Microsoft.EntityFrameworkCore.DbContextOptions IPTS.PropertyManagement.Infrastructure.Data.PropertyContext”

*有时公司使用 SQL 服务器,有时使用 InMemory,或 SQLite

单元测试失败:

services.AddSingleton(a =>
{
    var mock = new Mock<IUserResolverService>();
    mock.Setup(b => b.GetUser()).Returns(5);
    return mock.Object;
});

services.AddDbContext<PropertyContext>(
    options => options.UseInMemoryDatabase("Ipts").UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll),
    ServiceLifetime.Singleton);
services.AddSingleton<DbContext, PropertyContext>();


services.AddDbContext<AuditablePropertyContext>(
    options => options.UseInMemoryDatabase("Ipts").UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll),
    ServiceLifetime.Singleton);
services.AddSingleton<AuditablePropertyContext>();

services.RegisterMappingProfiles(new ApplicationServicesMappingProfile(),
    new PropertyManagementDataMappingProfile());
return services;

}

单元测试:错误

Message: 
System.InvalidOperationException : No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
Stack Trace: 
DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
DbContext.get_InternalServiceProvider()
DbContext.get_DbContextDependencies()

将构造函数PropertyContext class 更改为以下代码:

public PropertyContext(DbContextOptions options)
        : base(options)
    {
    }

然后将构造函数AuditablePropertyContext class 更改为以下代码:

  public AuditablePropertyContext(DbContextOptions options, UserResolverService userService)
        : base(options)
    {
        _user = userService.GetUser();
    }

注意:不需要时删除两个类中的默认构造函数。

您还可以仅在具体子类型上提供专门的DbContextOptions<Repo>

例如

public abstract class BaseRepo: DbContext
{
    public BaseRepo(DbContextOptions options) : base(options)
    {

    }
}


public sealed class Repo : BaseRepo
{
    public Repo(DbContextOptions<Repo> options) : base(options)
    {

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM