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
が自動的に依存関係をサービスプロバイダに注入することを期待しています。
メモリデータベースがであるため、エラーは混乱します。
これは私が直面している正確なエラーです。しかし、私はEntityFramework.InMemory v2.0.0を使用していて、 'UseInMemoryDatabase()'は廃止されました。あなたはドキュメントが限られているので、 'services.AddEntityFrameworkInMemoryDatabase()。AddDbContext <'はそれを行う新しい方法です。私はこれを持っているすべての方法が、私はまだエラーが発生します。昨年、あなたはNugetパッケージをアップグレードしたことがありますか?ありがとう。 –
@JeremyThompson私は自分のプロジェクトを非常に遠くにしてしまったわけではありません。それは何よりも.netcoreを使った実験のほうが多かった。 '.AddEntityFrameworkInMemoryDatabase'がそれを行う*新しい方法であるかどうかは分かりません。 'options.UseInMemoryDatabase()'はメモリ内のプロバイダを使うようにコンテキストを設定し、 '.AddEntityFrameworkInMemoryDatabase()'は依存性注入を設定するので、両方を指定する必要があります。私は家にいるときに、最新のものにアップグレードした後に見ていきます。 – Rob
[この質問](https://stackoverflow.com/questions/43098065)または[this one](https:// stackoverflow。com/questions/43991088)があなたの問題に関連している可能性があります – Rob