相关疑难解决方法(0)

ASP.Net vNext DbContext依赖注入多个请求问题.

我试图使用ASP.Net vNext,MVC,EF7和存储库模式(这里不是问题,我不认为)......

我遇到的问题是,当对数据库发出多个请求时,我收到以下错误:"已经有一个与此命令关联的打开的DataReader必须先关闭."

这是一些代码:

public class Startup
{
    public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        Configuration = new Configuration().AddJsonFile("config.json").AddEnvironmentVariables();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        // Register Entity Framework
        services.AddEntityFramework(Configuration)
            .AddSqlServer()
            .AddDbContext<MyDbContext>();

        services.AddSingleton<ILocationRepo, LocationRepo>();
        services.AddSingleton<IStateRepo, StateRepo>();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();

        var testData = ActivatorUtilities.CreateInstance<TestData>(app.ApplicationServices);
        testData.InitializeData();
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

[Route("api/[controller]")]
public class LocationsController : Controller
{
    private readonly ILocationRepo _repo;

    public LocationsController(ILocationRepo repo)
    {
        _repo = repo;
    }

    // GET: api/locations
    [HttpGet]
    public …
Run Code Online (Sandbox Code Playgroud)

dependency-injection repository-pattern asp.net-core-mvc asp.net-core

0
推荐指数
1
解决办法
1238
查看次数