首页 > 解决方案 > BaseController 中的 EF Core 上下文始终为空

问题描述

我有一个BaseController但是当我从子控制器调用函数时,上下文总是null

public class BaseController : Controller
{
    private readonly TheContext _tContext;
    public BaseController(TheContext tContext)
    {
        _tContext= tContext;
    }

    public BaseController() { }

    public void test()
    {
        var foo = _tContext.BAR;
    }
}

我一直在通过将上下文BaseControler从子控制器传递给函数来处理这个问题。

但是,我现在需要使用OnActionExecuting(),因为我需要为每个使用BaseController.

标签: c#entity-frameworkasp.net-core-3.1dbcontextonactionexecuting

解决方案


推荐阅读