[英]LINQPad EF Core no database provider has been configured for this 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.