DbContext实例不能在OnConfiguring内部使用,因为此时它仍在配置中 [英] A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point

查看:1120
本文介绍了DbContext实例不能在OnConfiguring内部使用,因为此时它仍在配置中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建自定义视图位置系统.

I'm trying to build a custom view location system.

public class myViewLocationExpander : IViewLocationExpander
{
    private myDBContext _context;

    public myViewLocationExpander (myDBContext context)
    {
        _context = context;
    }

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        _context.Property.//..... //Error happened here
           Some other codes
    }

在我的启动文件中

    public void ConfigureServices(IServiceCollection services)
    {
       //other codes

        services.AddMvc();

        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.ViewLocationExpanders.Add(new myViewLocationExpander (new myDBContext()));
        });

我的错误1:

处理请求时发生未处理的异常. InvalidOperationException:没有配置数据库提供程序.在设置服务时,通过覆盖DbContext类或AddDbContext方法中的OnConfiguring来配置数据库提供程序. Microsoft.Data.Entity.Internal.DatabaseProviderSelector.SelectServices(ServiceProviderSource providerSource)

An unhandled exception occurred while processing the request. InvalidOperationException: No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services. Microsoft.Data.Entity.Internal.DatabaseProviderSelector.SelectServices(ServiceProviderSource providerSource)

错误2:

EntityFramework.Core.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理 附加信息:尝试在配置上下文时使用上下文. DbContext实例不能在OnConfiguring内部使用,因为此时仍在配置它.

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.Core.dll but was not handled in user code Additional information: An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point.

如何在类中使用dbcontext(我需要从数据库中获取一些信息),该类应放在启动文件的Configure()方法上.

How can I use dbcontext (I need to get some infos from DB) in a class which should be place on Configure() method on Startup file.

还是可以将IViewLocation ..放到另一个地方?

Or can I put IViewLocation.. to another place?

推荐答案

在IViewLocationExpander的代码中,您可以使用以下代码context.ActionContext.HttpContext.ApplicationServices.GetService( typeof( myDbContext ) )从上下文中获取IServiceProvider,这将返回您的DbContext.

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;
    }
}

我注意到它第二次调用PopulateValues时才有效,但这不是我第一次没有时间对此进行进一步的研究

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

这篇关于DbContext实例不能在OnConfiguring内部使用,因为此时它仍在配置中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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