简体   繁体   English

如何使用实体框架和 Oracle 托管数据访问连接到 Oracle 数据库?

[英]How can I connect to a Oracle Database using Entity Framework and Oracle Managed Data Access?

I want to connect to a Oracle database using Entity Framework version 6.1.3.我想使用实体框架 6.1.3 版连接到 Oracle 数据库。 I'm working with Oracle Managed Data Acces package for Nuget in it's version 19.5.0 and Oracle Managed Data Access Entity Framework Nuget package in it's 19.3.0. I'm working with Oracle Managed Data Acces package for Nuget in it's version 19.5.0 and Oracle Managed Data Access Entity Framework Nuget package in it's 19.3.0. Also I'm using Visual Studio 2019. I want to create entities from the database tables using ADO .NET option in VS, but when I try to use this option and click in the Code First from database option , it doesn't show me any connection to oracle database.我也在使用 Visual Studio 2019。我想使用 VS 中的ADO .NET选项从数据库表中创建实体,但是当我尝试使用此选项并单击Code First from database option ,它没有显示任何连接到 oracle 数据库。 I have my App.config file like this:我有这样的 App.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework"
      type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      requirePermission="false"/>
    <section name="oracle.manageddataaccess.client"
      type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
      <provider invariantName="Oracle.ManagedDataAccess.Client"
        type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client"/>
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
        type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <publisherPolicy apply="no"/>
        <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
        <bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.122.19.1"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
      </dataSources>
    </version>
  </oracle.manageddataaccess.client>
  <connectionStrings>
    <add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client"
      connectionString="User Id=user;Password=password;Data Source=oracle"/>
  </connectionStrings>
</configuration>

I don't know if the problem comes from this file or from another part.我不知道问题是来自这个文件还是来自其他部分。 Also I have tried creating a new Data Connection , but when I select the Oracle Database option, it says that the provider is deprecated and I have to install Oracle Developer Tools for Visual Studio .我也尝试过创建一个新的Data Connection ,但是当我 select Oracle Database选项时,它说提供程序已被弃用,我必须Oracle Developer Tools for Visual Studio I also download this and nothing changes.我也下载了这个,没有任何变化。 How can I solve this?我该如何解决这个问题?

The solution I take to solve this is to uninstall VS 2019 and start to use VS 2017. This is because the ODAC drivers are only compatible with VS 2017 and older versions.我采取的解决方案是卸载 VS 2019 并开始使用 VS 2017。这是因为 ODAC 驱动程序仅与 VS 2017 及更旧版本兼容。 Once I do this I was given the option to create a database using ODP .NET driver in the section of Server Explorer located in View -> Server Explorer -> Righ Click in Data Connections -> Add Connection完成此操作后,我可以选择使用ODP .NET驱动程序创建数据库,位于 View -> Server Server Explorer -> Right Click in Data Connections -> Add Connection

EDIT Now there are the ODAC drivers to work with Visual Studio 2019编辑现在有用于 Visual Studio 2019 的 ODAC 驱动程序

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

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