在 ServiceStack IoC 中注入时,Dbcontext IDbset 属性为空
Dbcontext IDbset properties are null when injected in ServiceStack IoC
我已经在 ServiceStack 的标准容器中注册了我的 DbContext,DbContext 被注入到服务中但奇怪的是 IDbSet
属性 是 null
,所有其他属性都是预期。
如果我在服务构造函数中新建 DbContext,IDbSet
属性 会如您所料实例化,只是为了证明 EF 的配置已正确设置。所有其他已注册的服务都以这种方式正确注入,没有这种奇怪的行为。
我错过了什么?
public class AppHost : AppHostBase
{
public override void Configure(Container container)
{
container.AddScoped<IDbContext, MyDataContext>();
// Other registrations here omitted for brevity.
}
}
public interface IDbContext : IDisposable
{
IDbSet<MyEntity> MyEntity { get; set; }
DbChangeTracker ChangeTracker { get; }
DbContextConfiguration Configuration { get; }
Database Database { get; }
int SaveChanges();
Task<int> SaveChangesAsync();
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
DbEntityEntry<T> Entry<T>(T entity) where T : class;
}
我已经通过像这样更改注册来解决这个问题;
container.Register<IDbContext>(i => new MyDataContext()).ReusedWithin(ReuseScope.Request);
我的原始注册 container.AddScoped<IDbContext, MyDataContext>();
是 shorthand 自动注册类型; container.RegisterAutoWiredAs<MyDataContext,IDbContext>().ReusedWithin(ReuseScope.Request);
我想知道 Func 容器在使用 AutoWired 时是否正在尝试解析 iDbSet 属性。这可以解释为什么它们最终为空。
我已经在 ServiceStack 的标准容器中注册了我的 DbContext,DbContext 被注入到服务中但奇怪的是 IDbSet
属性 是 null
,所有其他属性都是预期。
如果我在服务构造函数中新建 DbContext,IDbSet
属性 会如您所料实例化,只是为了证明 EF 的配置已正确设置。所有其他已注册的服务都以这种方式正确注入,没有这种奇怪的行为。
我错过了什么?
public class AppHost : AppHostBase
{
public override void Configure(Container container)
{
container.AddScoped<IDbContext, MyDataContext>();
// Other registrations here omitted for brevity.
}
}
public interface IDbContext : IDisposable
{
IDbSet<MyEntity> MyEntity { get; set; }
DbChangeTracker ChangeTracker { get; }
DbContextConfiguration Configuration { get; }
Database Database { get; }
int SaveChanges();
Task<int> SaveChangesAsync();
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
DbEntityEntry<T> Entry<T>(T entity) where T : class;
}
我已经通过像这样更改注册来解决这个问题;
container.Register<IDbContext>(i => new MyDataContext()).ReusedWithin(ReuseScope.Request);
我的原始注册 container.AddScoped<IDbContext, MyDataContext>();
是 shorthand 自动注册类型; container.RegisterAutoWiredAs<MyDataContext,IDbContext>().ReusedWithin(ReuseScope.Request);
我想知道 Func 容器在使用 AutoWired 时是否正在尝试解析 iDbSet 属性。这可以解释为什么它们最终为空。