我想将DbContext注入所有控制器。
public abstract class MyController: Controller
{
public MyDbContext db { get; set; }
}
protected void Application_Start()
{
[..]
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
builder.RegisterType<MyDbContext>().InstancePerHttpRequest();
}
但是,当我在任何控制器中访问base.db时,它为null。 我在这里缺少什么?