EF Core如何在类实例中获取DBContext? [英] EF Core how to get a DBContext in a class instance?
问题描述
ASP NET Core项目使用实体框架核心.
The ASP NET Core project uses the Entity Framework Core.
从Home控制器的方法中,我调用Task.Run,以获取类 MyClass
的实例的方法,在这里我需要从数据库访问数据.
From the method of the Home controller I call Task.Run for the method of the instance of the class MyClass
, where I need to access the data from the database.
我试图将 _dbContext
传递给 MyClass
类的构造函数,但出现异常
I tried to pass _dbContext
to the constructor of the MyClass
class, but I get the exception
无法访问已处置的对象.
Cannot access a disposed object.
HomeController
[Route("api/[controller]")]
[EnableCors("AllowAllOrigin")]
[ApiController]
public class HomeController : ControllerBase
{
private readonly DataBaseContext _dbContext;
public HomeController (DataBaseContext dbContext)
{
_dbContext = dbContext;
}
[EnableCors("AllowAllOrigin")]
[HttpPost("[action]")]
public string UpdateBotSettings()
{
MyClass myClass = new MyClass(_dbContext);
Task task = Task.Run(() => myClass.AnyMethod());
}
}
MyClass
public class MyClass
{
private readonly DataBaseContext _dbContext;
public MyClass(DataBaseContext dbContext)
{
_dbContext = dbContext;
}
public async void AnyMethodAsync()
{
MyData myData = _dbContext.AnyData.Where(d => d.id == 1).FirstOrDefault(); // <- exception
}
}
启动
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
string connection = Configuration.GetConnectionString("ConnectionDB");
services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connection));
// another code
}
能否请您告诉我如何在MyClass实例内部访问dbContext?
Can you please tell me how to access dbContext inside an instance of MyClass?
推荐答案
代码中最重要的错误是您在 AnyMethodAsync()
方法中使用了 async void
Myclass
的代码.不要在代码中使用 async void
,而是使用 async Task
.详细信息如下: C#–注意您的代码中的异步无效
Most important mistake in your code is that you are using async void
in your AnyMethodAsync()
method of the Myclass
. Don't use async void
in your code, instead use async Task
. Here is the details: C# – beware of async void in your code
因此,如下所示编写您的 AnyMethodAsync()
:
So write your AnyMethodAsync()
as follows:
public async Task AnyMethodAsync()
{
MyData myData = await _dbContext.AnyData.Where(d => d.id == 1).FirstOrDefault();
}
现在,您可以如下所示将 MyClass
注册到ASP.NET Core DI容器:
Now you can register your MyClass
to the ASP.NET Core DI container as follows:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<MyClass>(); // <-- here it is
string connection = Configuration.GetConnectionString("ConnectionDB");
services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connection));
// another code
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
现在按如下方式使用它:
Now use it as follows:
[Route("api/[controller]")]
[EnableCors("AllowAllOrigin")]
[ApiController]
public class HomeController : ControllerBase
{
private readonly DataBaseContext _dbContext;
private readonly MyClass _myClass
public HomeController (DataBaseContext dbContext, MyClass myClass)
{
_dbContext = dbContext;
_myClass = myClass;
}
[EnableCors("AllowAllOrigin")]
[HttpPost("[action]")]
public async Task<string> UpdateBotSettings()
{
await _myClass.AnyMethod();
// rest of codes
}
}
这篇关于EF Core如何在类实例中获取DBContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!