gpt4 book ai didi

c# - 如何在 Controller 之外使用 EF DbContext?

转载 作者:行者123 更新时间:2023-11-30 17:32:57 25 4
gpt4 key购买 nike

我正在构建一个 ASP.Net 应用程序,它是一个模拟器,许多操作将作为单独的威胁在 Controller 之外发生,但我正在努力在 Controller 之外创建 DbContext 连接。我知道我可以使用 OnConfiguring(DbContextOptionsBuilder optionsBuilder) 但我认为这不是最优雅的方式。

我通过这样的事情应该有效:

public class SimRepository : ISimRepository
{
public IConfiguration _configuration;
public DbContextOptionsBuilder _dbOptions;

public SimRepository(IConfiguration configuration)
{
_configuration = configuration;

_dbOptions = new DbContextOptionsBuilder();
_dbOptions.UseSqlServer(_configuration.GetConnectionString("DefaultConnection"));
}

public void LatheIn()
{
using (AppDbContext db = new AppDbContext(_dbOptions.Options))
{

}
}
}

但我收到此错误:

Argument 1: cannot convert from Microsoft.EntityFrameworkCore.DbContextOptions' to Microsoft.EntityFrameworkCore.DbContextOptions<Simulator_Line.Server.AppDbContext>

在启动中:

public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();

//Enable use of appsettings.json outside of Startup.cs
services.AddSingleton(Configuration);
services.AddSingleton<IConfiguration>(Configuration);
}

如果我的方法不正确,我愿意接受更改。

编辑

    public class LatheController : Controller
{
private readonly AppDbContext _dbContext;
private readonly SimRepository _simRrepo;

public LatheController(AppDbContext dbContext, SimRepository latheRepo)
{
_dbContext = dbContext;
_simRrepo = latheRepo;
}

[HttpGet]
public IEnumerable<Lathe> GetLathes()
{
_simRrepo.LatheIn();



return _dbContext.Lathes;
}
}

最佳答案

您已经在服务集合中注册了 DbContext。直接注入(inject)到依赖类中,这样在依赖类解析的时候就可以解析了

public class SimRepository : ISimRepository {
public readonly AppDbContext db;

public SimRepository(AppDbContext db) {
this.db = db;
}

public void LatheIn() {
//...use db
}
}

更新

你可以创建一个工厂

public interface IAppDbContextFactory {
AppDbContext Create();
}

根据需要用于创建实例

public class SimRepository : ISimRepository {
public readonly IAppDbContextFactory factory;

public SimRepository(IAppDbContextFactory factory) {
this.factory = factory;
}

public void LatheIn() {
using (AppDbContext db = factory.Create()) {
//...use db
}
}
}

工厂实现看起来像

public class AppDbContextFactory : IAppDbContextFactory {
private readonly IServiceProvider servideProvider;

public AppDbContextFactory(IServiceProvider servideProvider) {
this.serviceProvider = serviceProvider;
}

public AppDbContext Create() {
return serviceProvider.GetService<AppDbContext>();
}
}

确保将您的类型注册到服务集合。

public void ConfigureServices(IServiceCollection services) {
// Add framework services.
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();

//Enable use of appsettings.json outside of Startup.cs
services.AddSingleton(Configuration);
services.AddSingleton<IConfiguration>(Configuration);

services.AddSingleton<IAppDbContextFactory, AppDbContextFactory>();
services.AddTransient<ISimRepository, SimRepository>();
}

以便在需要的地方使用

public class SomeController : Controller {
private readonly ISimRepository simRepository;
public SomeController(ISimRepository simRepository) {
this.simRepository = simRepository;
}

public IActionResult SomeAction() {
simRepository.LatheIn();
return Ok();
}
}

关于c# - 如何在 Controller 之外使用 EF DbContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45236783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com