【问题标题】:UseInMemoryDatabase with UseInternalServiceProvider. No database provider configuredUseInMemoryDatabase 和 UseInternalServiceProvider。未配置数据库提供程序
【发布时间】:2017-03-27 22:32:26
【问题描述】:

我在使用 EntityFrameworkCore 时无法注入自定义 IAsyncQueryProvider。更准确地说..在使用提供的内存数据库功能时,我无法注入提供程序。使用默认提供程序 (SqlServer),一切正常。

这是我的全球Startup.cs

private void ConfigureEntityFrameworkWithSecurity(IServiceCollection services)
{
    services
        .AddEntityFramework()
        .AddEntityFrameworkSqlServer()
        .AddScoped<IAsyncQueryProvider, CustomEntityProvider>()
        .AddDbContext<APIContext>((sp, options) =>
        {
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
                .UseInternalServiceProvider(sp);
        });
}

完美地工作,我可以在CustomEntityProvider 中放置一个断点来验证它确实被注入了。目前,CustomEntityProvider 只是简单地实现了IAsyncQueryProvider,并且只是通过请求。里面没有逻辑。

当我运行测试时,我将 webhost 配置为使用不同的 Startup 文件:

public class TestStartup : Startup
{
    public TestStartup(IHostingEnvironment env) : base(env)
    {
    }

    public override void ConfigureServices(IServiceCollection services)
    {
        services
            .AddDbContext<APIContext>((sp, options) =>
            {
                options.UseInMemoryDatabase()
                    .UseInternalServiceProvider(sp);
            });
        base.ConfigureServices(services);
    }
}

使用TestStartup 运行测试会产生错误:

System.InvalidOperationException:没有为此 DbContext 配置数据库提供程序。可以通过重写 DbContext.OnConfiguring 方法或在应用程序服务提供者上使用 AddDbContext 来配置提供者。如果使用了 AddDbContext,那么还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数。

并且APIContext 的定义是正确的:

public class APIContext : DbContext
{
    public APIContext(DbContextOptions<APIContext> options)
        : base(options)
    {
    }
    ...
}

TestStartup 中删除UseInternalServiceProvider 可以正常工作-但是,我不希望我的测试命中实际数据库。此外,我希望UseInMemoryDatabase 能够自动将依赖项注入到服务提供者中——因为它本身就可以很好地工作。

该错误令人困惑,因为内存数据库我要使用的提供程序。

【问题讨论】:

    标签: c# entity-framework asp.net-core entity-framework-core


    【解决方案1】:

    不幸的是,解决方案非常简单。但是,似乎很少有关于在内存数据库功能中使用依赖注入的文档。它似乎是其中之一。希望这个问题能为未来不幸遇到此问题的人提供帮助。

    我下载了EntityFramework源码进行调查,发现调用UseInMemoryDatabase会创建一个扩展InMemoryOptionsExtension,它本身会添加到服务提供者中,即:

    public virtual void ApplyServices(IServiceCollection services)
    {
        Check.NotNull(services, nameof(services));
    
        services.AddEntityFrameworkInMemoryDatabase();
    }
    

    而且解决方案看起来很简单:

    public class TestStartup : Startup
    {
        public TestStartup(IHostingEnvironment env) : base(env)
        {
        }
    
        public override void ConfigureServices(IServiceCollection services)
        {
            services
                .AddEntityFrameworkInMemoryDatabase()
                .AddDbContext<APIContext>((sp, options) =>
                {
                    options.UseInMemoryDatabase().UseInternalServiceProvider(sp);
                });
            base.ConfigureServices(services);
        }
    }
    

    【讨论】:

    • 这是我遇到的确切错误。但是我使用的是 EntityFramework.InMemory v2.0.0 并且UseInMemoryDatabase() 已经过时了。你是对的,文档是有限的,我猜services.AddEntityFrameworkInMemoryDatabase().AddDbContext&lt; 是这样做的新方法。虽然我有这种方式,但我仍然得到错误。你有没有机会在去年升级 Nuget 包?谢谢。
    • @JeremyThompson 我的项目并没有走得太远;这更像是一个 .netcore 的实验,而不是其他任何东西。我不确定.AddEntityFrameworkInMemoryDatabase 是否是 new 方法。据我记得,options.UseInMemoryDatabase() 配置上下文以使用内存提供程序,而.AddEntityFrameworkInMemoryDatabase() 配置依赖注入 - 所以两者都需要指定。升级到最新版本后,我回家看看。
    • This questionthis one 可能与您遇到的问题有关
    • @JeremyThompson 不幸的是,我无法让解决方案进入工作状态,以便在 2.0.0 版上试用。以上链接有帮助吗?
    • 感谢您的帮助 Rob,此答案显示了正确的方法:stackoverflow.com/a/38897560/495455 - 在大多数午餐时间欠您一杯啤酒@The Nixon Bar。
    【解决方案2】:

    单击解决方案资源管理器中的 -> 属性文件夹。 打开 -> launchSettings.json 文件 api/TodoItems

    “个人资料”:{ “IIS 快递”:{ "commandName": "IISExpress", “启动浏览器”:是的, "launchUrl": "swagger", “环境变量”: { "ASPNETCORE_ENVIRONMENT": "发展" } },

    “个人资料”:{ “IIS 快递”:{ "commandName": "IISExpress", “启动浏览器”:是的, "launchUrl": "api/TodoItems", “环境变量”: { "ASPNETCORE_ENVIRONMENT": "发展" } },

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-02
      • 2019-09-27
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2021-05-29
      • 2021-05-04
      相关资源
      最近更新 更多