问题描述
我正在尝试使用 scrutor 库装饰 DbContext,以便我可以将一些代码注入 SaveChanges()
。
我的装饰器看起来像这样:
public class DbContextDecorator : DbContext
{
private readonly DbContext _context;
public DbContextDecorator(DbContext context)
{
_context = context
}
public override int SaveChanges()
{
_context.SaveChanges();
// some other code
}
}
我正在使用它
builder.Services.Decorate(typeof(MyContext),typeof(DbContextDecorator));
但是,当我想在构造函数中或使用 MyContext
检索我装饰的 GetService<MyContext>()
时,它会永远挂起,看起来像是死锁或什么。
我也尝试将我的装饰器类更改为
public class DbContextDecorator<TContext> : DbContext where TContext : DbContext
{
private readonly TContext _context;
public DbContextDecorator(TContext context)
{
_context = context
}
}
装饰得像
builder.Services.Decorate(typeof(MyContext),typeof(DbContextDecorator<MyContext>));
但它是一样的,这只是我认为可行的错误希望。
我是不是做错了什么,还是不可能以这种方式装饰 DbContext 而我需要为它创建接口?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)