EntityFramework,配合Oracle.ManagedDataAccess配置Oracle数据库连接

2019/03/14 20:37
阅读数 772

1.环境:

windows10,vs2017,oracle 11g(32 bit),PLSQL Developer 13 (32 bit)。

2.准备工具:

下载oracle 11g,地址:https://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win32soft-098987.html

下载ODAC for Visual Studio 2017,地址:https://www.oracle.com/technetwork/topics/dotnet/downloads/odacmsidownloadvs2017-3806459.html

下载PLSQL Developer 13(32 bit) ,地址:https://www.allroundautomations.com/bodyplsqldevreg.html

3.安装:

先oracle 11g安装;

然后安装PLSQL Developer 13(32 bit) ;

最后安装ODAC for Visual Studio 2017;

4.配置

  • 安装完ODAC for Visual Studio 2017后VS扩展内有多两个DLL,直接在项目中引用

  • 在映射文件中将数据映射字段,及表名全部改成大写。

5.配置config文件

加入如下节点,注意版本号

 

<oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="xxxx" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xxxx))) " />
      </dataSources>
      <edmMappings>
        <edmMapping dataType="number">
          <add name="bool" precision="1" />
          <add name="byte" precision="2" />
          <add name="int16" precision="5" />
        </edmMapping>
      </edmMappings>
    </version>
  </oracle.manageddataaccess.client>
  <connectionStrings>
    <add name="xxxx" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=xxxx;Password=xxxx;Data Source=xxxx" />
  </connectionStrings>

注意这边的Data Source与xml中节点的名称一样<dataSource /> ,这里的<edmMappings>还可以增加int32,及int64。

若出现连接错误,可以在connectionString中加入Persist Security Info=True;

<entityFramework>
    <!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />-->
    <defaultConnectionFactory type="Oracle.ManagedDataAccess.EntityFramework.OracleConnectionFactory, Oracle.ManagedDataAccess.EntityFramework, Version=6.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    <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.1.0, 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.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </DbProviderFactories>
  </system.data>
<configSections>
    <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
  </configSections>

来个完整点的,但是这个没有数据连接节点

<?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.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
  </configSections>
  <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.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="Oracle.ManagedDataAccess.EntityFramework.OracleConnectionFactory, Oracle.ManagedDataAccess.EntityFramework, Version=6.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    <providers>
      <provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
</configuration>
View Code

6.代码修改

在DbContext中OnModelCreating方法内增加一句

modelBuilder.HasDefaultSchema("xxxx");这里的xxxx是指用户名,我用的是SYSTEM。

 

 

配置完后可以使用EF的代码迁移进行建表。

7.踩坑指南:

  • oracle数据库32位的,那么PLSQL 也要32位的。
  • nuget上的Oracle.ManagedDataAccess及Oracle.ManagedDataAccess.EntityFramework我试了,不过我这边不能用,不知道其他人如何。
  • string类型的模型字段值,最好给个长度。不然在取值时会出现错误。
  • 数据库表名及属性长度30位
  • 使用ABP框架请在模块配置文件中配置(该配置很重要
  1. Database.SetInitializer(new CreateDatabaseIfNotExists<ServerOracleDbContext>());
  2. Configuration.UnitOfWork.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部