A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 06:17:52

In the code for your IViewLocationExpander you can get IServiceProvider from the context using the following code context.ActionContext.HttpContext.ApplicationServices.GetService( typeof( myDbContext ) ) this will return your DbContext.

public class MyViewLocationExpander : IViewLocationExpander
{
    public void PopulateValues( ViewLocationExpanderContext context )
    {
        var dbContext = context.ActionContext.HttpContext.ApplicationServices.GetService<ApplicationDbContext>();
    }

    public IEnumerable<string> ExpandViewLocations( ViewLocationExpanderContext context, IEnumerable<string> viewLocations )
    {
        var dbContext = context.ActionContext.HttpContext.ApplicationServices.GetService<ApplicationDbContext>();
        return viewLocations;
    }
}

I noticed that it works the second time PopulateValues is called just not the first time I have had no time to look further into this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!