使用WEBAPI连接到ORACLE配置流程
1.引用DLL
Oracle.ManagedDataAccess.dll
Oracle.ManagedDataAccess.EntityFramework.dll
下载地址:http://www.oracle.com/technetwork/database/windows/downloads/index-090165.html
2.修改Web.Config
<configSections>
<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.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />-->
</configSections>
<entityFramework>
<defaultConnectionFactory type="Oracle.ManagedDataAccess.EntityFramework.OracleConnectionFactory,
Oracle.ManagedDataAccess.EntityFramework,
Version=6.121.2.0,
Culture=neutral,
PublicKeyToken=89b483f429c47342" />
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.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.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
</system.data>
3.连接字符串配置
3.1 配置连接字符串方式
<connectionStrings>
<add name="OraString" connectionString="Data Source= (DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = *****)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORCL)
)
);User ID=*****;Password=*****;Persist Security Info=True" providerName="Oracle.ManagedDataAccess.Client" />
</connectionStrings>
3.2 使用TNS配置连接(即tnsnames.ora配置文件)
<oracle.manageddataaccess.client>
<version number="*">
<settings>
<setting name="配置连接名" value="ora文件路径" />
<setting name="TraceOption" value="1" />
<setting name="PerformanceCounters" value="0" />
</settings>
</version>
</oracle.manageddataaccess.client>
3.3 如果本机安装了oracle,需要检查以下配置字段,如果有修改版本号Version 为当前Oracle.ManagedDataAccess.Client版本。
<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
3.遇到的问题
故障:ORA-01918: 用户 'dbo' 不存在
原因:EF Provider未切换到换成支持Oracle
解决方法:在上下文里重载OnModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("这里写你的Oracle用户名,一定要大写噢!");
}
位置:上下文一般为:“
应用名.cs
”, 特征为继承了: DbContext类,可以Ctrl+F全局搜索“: DbContext”找到具体位置。
注意事项:完成后,需要重新使用Enable-Migrations重新刷新一下edmx。
有关转载原文以及推荐:
https://www.cnblogs.com/wlflovenet/p/4187455.html
http://www.bubuko.com/infodetail-2002598.html