LoginSignup
0
1

More than 5 years have passed since last update.

Visual Studioの.NET単体テストで「すべて実行」するとEntityFrameworkエラーが発生する

Last updated at Posted at 2018-08-20

現象

  • 個別実行の単体テストは成功する
  • 「すべて実行」は失敗する
  • EntityFramework関連のエラーが発生する

エラーメッセージ例

"The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information."

または、

The Entity Framework provider type 'Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' registered in the application config file for the ADO.NET provider with invariant name 'Oracle.ManagedDataAccess.EntityFramework' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application

解決策

いくつか条件によって異なります。

  1. 正しくEntityFrameworkをnugetでインストールできているか確認する。
  2. web.config/app.configに正しく設定できているか確認する。
  3. [DeploymentItem]属性で外部ファイルを単体テストでロードしているか確認する。

3番目については正しく理解できていませんが、これを書いた途端にテストが失敗し出したので調べてみたら、どうやら関連がありそうでした。↓

DeploymentItem breaks EntityFramework unit test in separate assembly

とりあえず、意図的にSystem.Data.Entity.SqlServer.SqlProviderServicesをDbContextで宣言すればテストは動きそうです。
が、外部ファイルのプロパティで出力ディレクトリにコピーを「常にコピーする」にすれば単体テストで読み込めたので、いったん[DeploymentItem]を使わずにおきます。(単体テストを自動化した際にもうまく動くかは未確認です。)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1