C# Oracle Instant Client 和 Entity Framework 配置问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9911948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:12:01  来源:igfitidea点击:

Oracle Instant Client and Entity Framework trouble with configuration

c#asp.net-mvcoracleentity-frameworkodac

提问by Mustang31

I'm trying to learn and figure out if it is possible to deploy an MVC, EF, ODAC 11.2.0.3 app to a server that has a previous version of ODP.NET installed. Rather than updating the sever ODP.NET (which I can't), I figured I could use the Oracle Instant Client.

我正在尝试学习并弄清楚是否可以将 MVC、EF、ODAC 11.2.0.3 应用程序部署到安装了以前版本的 ODP.NET 的服务器上。与其更新服务器 ODP.NET(我不能),我想我可以使用 Oracle Instant Client。

Is this doable?

这是可行的吗?

1) I added these dlls to my project to support Instant Client

1) 我将这些 dll 添加到我的项目中以支持 Instant Client

-Oracle.DataAccess.dll

-Oracle.DataAccess.dll

-oci.dll

-oci.dll

-ociw32.dll

-ociw32.dll

-orannzsbb11.dll

-orannzsbb11.dll

-oraociei11.dll

-oraociei11.dll

-OraOps11w.dll

-OraOps11w.dll

2) Next I updated web.config for the dbProviderFactories

2)接下来我更新了 dbProviderFactories 的 web.config

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

3) This (afaik) is how to use the Oracle dll in the bin rathre than the GAC

3)这(afaik)是如何在bin而不是GAC中使用Oracle dll

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Oracle.DataAccess" publicKeyToken="89b483f429c47342" />
    <publisherPolicy apply="no" />
  </dependentAssembly>
</assemblyBinding>
</runtime>

4) Finally my connectionString

4)最后我的connectionString

    <connectionStrings>
    <add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=Oracle.DataAccess.Client;
provider connection string=&quot;DATA SOURCE=XXX;PASSWORD=XXX;PERSIST SECURITY INFO=True;USER ID=XXX&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

This is the error I receive Unable to find the requested .Net Framework Data Provider. It may not be installed.

这是我收到的错误无法找到请求的 .Net Framework 数据提供程序。它可能没有安装。

I really appreciate any help here. I'm rather new and have a lot to learn. Thanks in advance. cheers

我真的很感谢这里的任何帮助。我比较新,有很多东西要学。提前致谢。干杯

采纳答案by Jeremy

Add a <remove … />section in the <DbProviderFactories>element in the web config to remove any existing Oracle provider. (before the <add>)

在 web 配置<remove … /><DbProviderFactories>元素中添加一个部分以删除任何现有的 Oracle 提供程序。(在 之前<add>

<remove invariant ="Oracle.DataAccess.Client" />

<remove invariant ="Oracle.DataAccess.Client" />

回答by Arieh

It seems from your question that you need to deploy an update to your application and the new version of ODP.net using only xcopy deployment permission.

从您的问题来看,您似乎需要仅使用 xcopy 部署权限将更新部署到您的应用程序和 ODP.net 的新版本。

Since your application is being changed, then you shouldn't need the assembly binding changes or DbProviderFactories. Just update the csproj of the class library with your edmx etc to have a reference to the new ODP.net version, eg

由于您的应用程序正在更改,因此您不需要程序集绑定更改或 DbProviderFactories。只需使用您的 edmx 等更新类库的 csproj 即可引用新的 ODP.net 版本,例如

<Reference Include="Oracle.DataAccess, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86" />

If you get an issue with your tnsnames.ora, then you would have to do one of the following: a) Add a system environment variable TNS_ADMIN to point to the directory of the tnsnames.ora, or b) Change the connection string to something based on:

如果您的 tnsnames.ora 出现问题,那么您必须执行以下操作之一:a) 添加系统环境变量 TNS_ADMIN 以指向 tnsnames.ora 的目录,或 b) 将连接字符串更改为某些内容基于:

Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;

c) See if you can put a copy of the tnsnames.ora somewhere else.

c) 看看您是否可以将 tnsnames.ora 的副本放在其他地方。

回答by granadaCoder

Here is my Xcopy solution.

这是我的 Xcopy 解决方案。

I posted it over at

我把它贴在

(https://jeremybranham.wordpress.com/2011/04/25/oracle-instant-client-with-odp-net/#comment-181)

( https://jeremybranham.wordpress.com/2011/04/25/oracle-instant-client-with-odp-net/#comment-181)

as well.

以及。

But I think I can post my xml without formatting issues here.

但我想我可以在这里发布我的 xml 而不会出现格式问题。

Nuget "packages.config"

Nuget“packages.config”

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="CommonServiceLocator" version="1.0" targetFramework="net35" />
  <package id="EnterpriseLibrary.Common" version="5.0.505.0" targetFramework="net35" />
  <package id="EnterpriseLibrary.Data" version="5.0.505.0" targetFramework="net35" />
  <package id="EntLibContrib.Data.OdpNet" version="5.0.505.0" targetFramework="net35" />
  <package id="Unity" version="2.1.505.2" targetFramework="net35" />
  <package id="Unity.Interception" version="2.1.505.2" targetFramework="net35" />
</packages>

app.config

应用程序配置文件

(note the <clear />tag below. this may or may not be needed, but I figured it was better to clear them out since you don't know what may be in the machine.config file)

(注意<clear />下面的标签。这可能需要也可能不需要,但我认为最好清除它们,因为你不知道 machine.config 文件中可能有什么)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>


  <dataConfiguration defaultDatabase="OracleMainConnectionString">
  </dataConfiguration>

  <connectionStrings>

    <add name="OracleMainConnectionString"
         connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyOracleServerName)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=MyServiceName)));User ID=MyUserName;Password=MyPassword;"
      providerName="Oracle.DataAccess.Client" />
  </connectionStrings>

  <appSettings>
    <add key="ExampleKey" value="ExampleValue" />
  </appSettings>

  <system.data>
    <DbProviderFactories>
      <clear />
      <add name="Oracle Data Provider for .NET" invariant="Oracle.DataAccess.Client" description=".Net Framework Data Provider for Oracle" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
      <add name="EntLibContrib.Data.OdpNet" invariant="EntLibContrib.Data.OdpNet" description="EntLibContrib Data OdpNet Provider" type="EntLibContrib.Data.OdpNet.OracleDatabase, EntLibContrib.Data.OdpNet, Version=5.0.505.0, Culture=neutral, PublicKeyToken=null" />
    </DbProviderFactories>
  </system.data>


</configuration>

I am developing on a x64 Windows 7 machine.

我正在 x64 Windows 7 机器上开发。

I downloaded: ODAC1120320Xcopy_32bit.zip (from oracle.com)

我下载了:ODAC1120320Xcopy_32bit.zip(来自oracle.com)

Which is the:

哪一个是:

ODAC 11.2 Release 5 (11.2.0.3.20) Download the XCopy version [Released September 11, 2012]

ODAC 11.2 Release 5 (11.2.0.3.20) 下载 XCopy 版本 [2012 年 9 月 11 日发布]

I unzipped this zip file.

我解压了这个 zip 文件。

I searched and fished out these files:

我搜索并找出了这些文件:

oci.dll Oracle.DataAccess.dll orannzsbb11.dll oraociei11.dll OraOps11w.dll

oci.dll Oracle.DataAccess.dll orannzsbb11.dll oraociei11.dll OraOps11w.dll

Note, when there were 2 files of the same name, I took the "bin\2.x\" or "odp.net20\bin" version for my 3.5 Framework need (I'm not on 4.0 yet).

请注意,当有 2 个同名文件时,我使用了“bin\2.x\”或“odp.net20\bin”版本来满足我的 3.5 框架需求(我还没有使用 4.0)。

I took these files, and put them in a subfolder from where my .sln file resides.

我把这些文件放在我的 .sln 文件所在的子文件夹中。

.\MySolution.sln
.\MyConsoleApplicationFolder\MyConsoleApp.csproj
.\ThirdPartyReferences\
.\ThirdPartyReferences\Oracle\

I place all the files above in the

我把上面的所有文件放在

.\ThirdPartyReferences\Oracle\ 

folder

文件夹

I used "Add Reference" to add a reference to Oracle.DataAccess.dll to the "MyConsoleApp.csproj" csharp project. (This meant browsing to "..\ThirdPartyReferences\Oracle\" of course)

我使用“添加引用”向“MyConsoleApp.csproj”csharp 项目添加了对 Oracle.DataAccess.dll 的引用。(这当然意味着浏览到“..\ThirdPartyReferences\Oracle\”)

I used a "Post Build Event" to copy the "extra" (aka, "accessory)" files

我使用了“构建后事件”来复制“额外”(又名“附件)”文件

My lines in my post build event were:

我在后期构建事件中的台词是:

copy $(ProjectDir)..\ThirdPartyReferences\Oracle\oci.dll $(TargetDir)*.*
copy $(ProjectDir)..\ThirdPartyReferences\Oracle\orannzsbb11.dll $(TargetDir)*.*
copy $(ProjectDir)..\ThirdPartyReferences\Oracle\oraociei11.dll $(TargetDir)*.*
copy $(ProjectDir)..\ThirdPartyReferences\Oracle\OraOps11w.dll $(TargetDir)*.*

Note, my post build event replaces the "Copy if Newer" from the URL instructions above.

请注意,我的后期构建事件替换了上面 URL 说明中的“如果较新则复制”。

When I ran my project........I got a few missing dll errors.

当我运行我的项目时......我有一些缺少的 dll 错误。

Note: In the assembly that has your calls to the EnterpriseLibrary.Data objects…you'll get “Cannot find Microsoft.Practices.SomethingSomething namespace. Just keep adding references to these dll's (that the above package.config will pull down) until the errors go away.

注意:在调用 EnterpriseLibrary.Data 对象的程序集中……你会得到“无法找到 Microsoft.Practices.SomethingSomething 命名空间。只需继续添加对这些 dll 的引用(上面的 package.config 将下拉),直到错误消失。

Like here is a specific one:

就像这里是一个特定的:

"Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified."

So (after running Nuget of course, to download all the files) I went and added a reference to:

所以(当然,在运行 Nuget 之后,下载所有文件)我去添加了一个引用:

\packages\CommonServiceLocator.1.0\lib\NET35\Microsoft.Practices.ServiceLocation.dll

\packages\CommonServiceLocator.1.0\lib\NET35\Microsoft.Practices.ServiceLocation.dll

That cleared up the issues.

这就解决了问题。

And my csharp code: (note "select *" is for demo purposes only)

还有我的 csharp 代码:(注意“select *”仅用于演示目的)

/*
    using System;
    using System.Data;
    using System.Data.Common;
    using Microsoft.Practices.EnterpriseLibrary.Data;
 */
public IDataReader EmployeesGetAll()
{

    IDataReader returnReader = null;

    try
    {

        Database db = DatabaseFactory.CreateDatabase();
        DbCommand dbc = db.GetSqlStringCommand("SELECT * FROM ( SELECT * FROM TEMPLOYEE ) WHERE ROWNUM <= 25");
        returnReader = db.ExecuteReader(dbc);
        return returnReader;

    }

    finally
    {
    }

}

And it worked.

它奏效了。

Thank you:

谢谢:

https://jeremybranham.wordpress.com/2011/04/25/oracle-instant-client-with-odp-net/#comment-181

https://jeremybranham.wordpress.com/2011/04/25/oracle-instant-client-with-odp-net/#comment-181

I think this makes ODP.NET an "xcopy" deployment.

我认为这使 ODP.NET 成为“xcopy”部署。

I still need to test on a clean machine to be sure.

我仍然需要在干净的机器上进行测试才能确定。

But its the end of the day..............

但它的一天结束了.......

================

================

Additional Information:

附加信息:

Everything above is correct. However, I hit a caveat. I was using a "Console Application" to test my code.

以上都是对的。但是,我遇到了一个警告。我正在使用“控制台应用程序”来测试我的代码。

When you add a new Console Application to visual studio, it DEFAULTS to x86.

当您将新的控制台应用程序添加到 Visual Studio 时,它默认为 x86。

As seen here:

如这里所见:

http://www.xavierdecoster.com/post/2011/02/15/console-application-visual-studio-gotcha-on-x64-os-aspx

http://www.xavierdecoster.com/post/2011/02/15/console-application-visual-studio-gotcha-on-x64-os-aspx

EDIT: (Updated link)

编辑:(更新链接)

http://www.xavierdecoster.com/post/2011/02/15/console-application-visual-studio-gotcha-on-x64-os

http://www.xavierdecoster.com/post/2011/02/15/console-application-visual-studio-gotcha-on-x64-os

So when I put all the configuration and code and stuff in a real project (which was set to "Any CPU" on a x64 bit machine)...everything I had done stopped working. :<

因此,当我将所有配置、代码和内容放入一个实际项目(在 x64 位机器上设置为“任何 CPU”)时……我所做的一切都停止了。:<

After tweaking a bit........ I found this file on oracle.com ODAC1120320Xcopy_x64.zip I then repeated everything I did above , but searching the unzipped files of this x64 zip file.

稍作调整后……我在 oracle.com ODAC1120320Xcopy_x64.zip 上找到了这个文件,然后我重复了上面所做的一切,但搜索了这个 x64 zip 文件的解压缩文件。

Everything is working.

一切正常。

But that "x86" default thing with a Console application threw me for a loop.

但是带有控制台应用程序的“x86”默认设置让我陷入了困境。

回答by massiAng

I was getting the same error (data provider not found) when deploying ODP.NET via Instant Client. The only thing that I needed to do was to add the following to my exe.config file (inside the tag)

通过 Instant Client 部署 ODP.NET 时,我遇到了同样的错误(未找到数据提供程序)。我唯一需要做的就是将以下内容添加到我的 exe.config 文件中(在标签内)

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