简体   繁体   English

使用DbProviderFactory对Oracle对象进行单元测试中的System.InvalidCastException

[英]System.InvalidCastException in Unit Test with Oracle objects using DbProviderFactory

When using System.Data.Common.DbProviderFactory with we get this error when we run our code in an NUnit test but not when we run the regular web application. 与System.Data.Common.DbProviderFactory一起使用时,在NUnit测试中运行代码时会出现此错误,而在运行常规Web应用程序时则不会。

Error 错误

System.InvalidCastException : [A]Oracle.DataAccess.Client.OracleParameter cannot be cast to [B]Oracle.DataAccess.Client.OracleParameter. System.InvalidCastException:无法将[A] Oracle.DataAccess.Client.OracleParameter强制转换为[B] Oracle.DataAccess.Client.OracleParameter。 Type A originates from 'Oracle.DataAccess, Version=2.112.1.2, Culture=neutral, PublicKeyToken=89b483f429c47342' in the context 'Default' at location 'C:\\Windows\\assembly\\GAC_32\\Oracle.DataAccess\\2.112.1.2__89b483f429c47342\\Oracle.DataAccess.dll'. 类型A源自在位置'C'\\ Windows \\ assembly \\ GAC_32 \\ Oracle.DataAccess \\ 2.112.1.2__89b483f429c47342 \\的位置'Default'中的上下文'Default'中的'Oracle.DataAccess,Version = 2.112.1.2,Culture = neutral,PublicKeyToken = 89b483f429c47342' Oracle.DataAccess.dll”。 Type B originates from 'Oracle.DataAccess, Version=4.112.1.2, Culture=neutral, PublicKeyToken=89b483f429c47342' in the context 'Default' at location 'C:\\Windows\\Microsoft.Net\\assembly\\GAC_32\\Oracle.DataAccess\\v4.0_4.112.1.2__89b483f429c47342\\Oracle.DataAccess.dll'. 类型B源自'Oracle.DataAccess,Version = 4.112.1.2,Culture = neutral,PublicKeyToken = 89b483f429c47342'在位置'C'\\ Windows \\ Microsoft.Net \\ assembly \\ GAC_32 \\ Oracle.DataAccess \\ v4的上下文'Default'中.0_4.112.1.2__89b483f429c47342 \\ Oracle.DataAccess.dll'。

We have the 2.112.1.2 assembly referenced by the unit test project and we even tried 'use specific version' and 'copy local' but it just doesn't see that DLL it continues to load from a newer version in the GAC. 单元测试项目引用了2.112.1.2程序集,我们甚至尝试了“使用特定版本”和“复制本地”,但它只是没有看到DLL会继续从GAC中的较新版本加载。

Turns out the dll is not copied over to the executing folder of the test runner. 原来,dll没有复制到测试运行程序的执行文件夹中。 Luckily you can specify the specific dll to load out of the GAC. 幸运的是,您可以指定要从GAC加载的特定dll。

We added this to the app.config in the unit test project. 我们将此添加到了单元测试项目中的app.config中。 Note the specific assembly version in the type attribute of the <add /> element 注意 <add />元素的type属性中的特定程序集版本

<system.data>
    <DbProviderFactories>
      <clear />
      <add name="Oracle Data Provider" invariant="Oracle.DataAccess.Client" 
           description=".Net Framework Data Provider for Oracle" 
           type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.112.1.2, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </DbProviderFactories>
</system.data>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM