在ASP .Net Singleton注入类中使用DbContext
我需要在我的Startup类中实例化的Singleton类中访问我的数据库。 似乎直接注入它会导致处理掉的DbContext。
我收到以下错误:
无法访问已处置的对象。 对象名称:’MyDbContext’。
我的问题有两个:为什么这不起作用,如何在单例类实例中访问我的数据库?
这是我的Startup类中的ConfigureServices方法:
public void ConfigureServices(IServiceCollection services) { // code removed for brevity services.AddEntityFramework().AddSqlServer().AddDbContext( options => { var config = Configuration["Data:DefaultConnection:ConnectionString"]; options.UseSqlServer(config); }); // code removed for brevity services.AddSingleton(); }
这是我的控制器类:
public class TestController : Controller { private FunClass _fun; public TestController(FunClass fun) { _fun = fun; } public List Index() { return _fun.GetUsers(); } }
这是我的FunClass:
public class FunClass { private MyDbContext db; public FunClass(MyDbContext ctx) { db = ctx; } public List GetUsers() { var lst = db.Users.Select(c=>c.UserName).ToList(); return lst; } }
它不起作用的原因是因为.AddDbContext
扩展将每个请求添加为作用域。 每个请求的范围通常是您想要的,通常每个请求将调用一次保存更改,然后dbcontext
将在请求结束时处理。
如果你真的需要在singleton
使用dbContext
,那么你的FunClass
类应该依赖于IServiceProvider
和DbContextOptions
而不是直接依赖于DbContext
,这样你就可以自己创建它。
public class FunClass { private GMBaseContext db; public FunClass(IServiceProvider services, DbContextOptions dbOptions) { db = new GMBaseContext(services, dbOptions); } public List GetUsers() { var lst = db.Users.Select(c=>c.UserName).ToList(); return lst; } }
也就是说,我的建议是仔细考虑你是否真的需要你的FunClass成为一个单身人士,我会避免这种情况,除非你有充分的理由让它成为一个单身人士。
解决方案是调用AddSingleton,我的类在我的Startup类的method参数中实例化:
services.AddSingleton(s => new FunClass(new MyContext(null, Configuration["Data:DefaultConnection:ConnectionString"])));
解决方案是更改我的DbContext类:
public class MyContext : IdentityDbContext { private string connectionString; public MyContext() { } public MyContext(DbContextOptions options, string connectionString) { this.connectionString = connectionString; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { // Used when instantiating db context outside IoC if (connectionString != null) { var config = connectionString; optionsBuilder.UseSqlServer(config); } base.OnConfiguring(optionsBuilder); } }
然而,正如多人警告的那样,在单例类中使用DbContext可能是一个非常糟糕的主意。 我的用法在实际代码中非常有限(不是示例FunClass),但我认为如果你这样做,最好找到其他方法。
上述就是C#学习教程:在ASP .Net Singleton注入类中使用DbContext分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/998004.html