2016-11-14 8 views
3

EntityFrameworkCoreを使用している場合、カスタムIAsyncQueryProviderを注入できません。より正確に言えば、提供されたメモリ内データベース機能を使用する際に、プロバイダをインジェクトするのに問題があります。既定のプロバイダー(SqlServer)を使用すると、すべて正常に動作します。UseInMemoryDatabaseとUseInternalServiceProviderを使用します。データベースプロバイダが設定されていません

ここに私のグローバル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を実装し、単に要求を通過します。そこにはロジックは含まれていません。私は、テストを実行している場合は

、私がウェブホストが異なるStartupファイルを使用するように設定する:

System.InvalidOperationException : No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

そしてAPIContextが正しくあり:TestStartupでテストを実行

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

はエラーを生成します定義:

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

削除UseInternalServiceProviderからTestStartupまでは正常に動作しますが、私のテストは実際のデータベースには当てられません。さらに、私はUseInMemoryDatabaseが自動的に依存関係をサービスプロバイダに注入することを期待しています。

メモリデータベースであるため、エラーは混乱します。

答えて

5

残念なことに、このソリューションは、頭が痛むほど単純です。しかし、インメモリ・データベース機能で依存性注入を使用することについてのほとんどのドキュメントはないようです。それはどちらかと思われる。うまくいけば、この質問は、これに遭遇するほど不幸な将来の人々のための助けを提供するでしょう。

私が調査する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); 
    } 
} 
+0

これは私が直面している正確なエラーです。しかし、私はEntityFramework.InMemory v2.0.0を使用していて、 'UseInMemoryDatabase()'は廃止されました。あなたはドキュメントが限られているので、 'services.AddEntityFrameworkInMemoryDatabase()。AddDbContext <'はそれを行う新しい方法です。私はこれを持っているすべての方法が、私はまだエラーが発生します。昨年、あなたはNugetパッケージをアップグレードしたことがありますか?ありがとう。 –

+0

@JeremyThompson私は自分のプロジェクトを非常に遠くにしてしまったわけではありません。それは何よりも.netcoreを使った実験のほうが多かった。 '.AddEntityFrameworkInMemoryDatabase'がそれを行う*新しい方法であるかどうかは分かりません。 'options.UseInMemoryDatabase()'はメモリ内のプロバイダを使うようにコンテキストを設定し、 '.AddEntityFrameworkInMemoryDatabase()'は依存性注入を設定するので、両方を指定する必要があります。私は家にいるときに、最新のものにアップグレードした後に見ていきます。 – Rob

+0

[この質問](https://stackoverflow.com/questions/43098065)または[this one](https:// stackoverflow。com/questions/43991088)があなたの問題に関連している可能性があります – Rob

関連する問題