实体框架DbContext不使用依赖项注入来处置 [英] Entity Framework DbContext is NOT disposed using dependency injection

查看:72
本文介绍了实体框架DbContext不使用依赖项注入来处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解决方案中具有以下设置:

I have the following setup in a solution:

可以找到演示解决方案 此处

ASP.NET Core Web API:

ASP.NET Core Web API:

public class MyController : ControllerBase
{
    private readonly IMyService _service;
    public MyController(IMyService service)
        {
             _service = service;
        }
}

服务层:

public class MyService: IMyService, IDisposable
{
    private readonly IDataContext _context;
    public MyService(IDataContext context)
        {
             _context = context;
        }
}

实体框架核心:

public class DataContext : DbContext, IDataContext, IDisposable
{
    public DataContext(DbContextOptions<DataContext> options, IAuthenticationService authentication, ILogger<DataContext> logger) : base(options) 
        {
            ...
        }
}

使用Microsoft.Extensions.DependencyInjection将所有内容链接在一起的"CompositionRoot":

A 'CompositionRoot' to link it all together using Microsoft.Extensions.DependencyInjection:

services.AddDbContext<DataContext>(options =>  options.UseSqlServer(configuration.GetConnectionString("myConnection")), ServiceLifetime.Transient);
services.AddTransient<IMyService, MyService>();
//EDIT: removed this line
//services.AddTransient<IDataContext, DataContext>(); 

这一切都按预期工作,但是我的DataContext从未被丢弃.我已经在dispose方法中添加了日志来监视此行为,但是我无法弄清为什么会发生这种情况(或者在这种情况下不会发生).我已经尝试过

This all works as expected but my DataContext is never disposed. I've added logs in the dispose methods to monitor this behaviour but I can't figure out why this is happening (or not happening in this case). I've tried

  • 对"AddTransient"进行重新排序没有成功(如预期的那样)
  • 使用"AddScoped"而不是"AddTransient"

我正在使用一个接口来模拟单元测试中的DbContext,但是我希望将DbContext处理掉.有人知道为什么会发生这种情况以及如何解决吗?

I'm using an interface to mock the DbContext in my unit tests, but I want my DbContext to be disposed to. Anyone an idea on why this happens and how to solve it?

一些额外的日志

  • 2020-03-24 21:09:29.5727 |在DataContext中注入选项
  • 2020-03-24 21:09:29.6064 |身份验证已注入DataContext
  • 2020-03-24 21:09:29.6064 |记录器已注入DataContext
  • 2020-03-24 21:09:29.6262 | 创建的DataContext 1ddd98a1-a8f9-4096-8a11-c0b4d40d01ae
  • 2020-03-24 21:09:30.1918 |将记录器注入到CustomerService中
  • 2020-03-24 21:09:30.2200 | DataContext注入到CustomerService中
  • 2020-03-24 21:09:30.2300 |将映射程序注入到CustomerService中
  • 2020-03-24 21:09:30.2482 |认证已注入到CustomerService中
  • 2020-03-24 21:09:30.2482 | 创建了CustomerService 5b446267-d908-4291-9918-af1841324708
  • 2020-03-24 21:09:30.2769 |将记录器注入到CustomerController中
  • 2020-03-24 21:09:30.2769 | CustomerService已注入CustomerController
  • 2020-03-24 21:09:30.3186 | CustomerController.GetCustomer(4)
  • 2020-03-24 21:09:35.0599 | 部署CustomerService 5b446267-d908-4291-9918-af1841324708

编辑3月25日:我尝试使用不带接口的DataContext,但问题仍然存在.我真的不知道我在做什么错!

EDIT march 25: I've tried using the DataContext without an interface but the problem still exists. I realy have no clue what I'm doing wrong!

推荐答案

您没有正确注册DbContext

You are not registering the DbContext correctly

使用以下 AddDbContext

services.AddDbContext<IDataContext, DataContext>(options =>  
    options.UseSqlServer(configuration.GetConnectionString("myConnection")));

这会将界面/抽象与具体实现相关联.

Which will associate the interface/abstraction with the concrete implementation.

并删除暂时注册

services.AddTransient<IDataContext, DataContext>();

这篇关于实体框架DbContext不使用依赖项注入来处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆