解决方案中的数据层项目最初使用的是oracle 11g + ef5 创建的实体模型,在分页时遇到了skip参数为0报错的问题,没有找到相关资料。

于是决定升级到ef6,在oracle官网中得知,Oracle Data Provider for .NET in ODAC 12c Release 3 开始支持ef6(https://docs.oracle.com/cd/E56485_01/win.121/e55744/release_changes.htm#CIHGIAEG)

安装步骤:

1.安装odac,下载地址http://www.oracle.com/technetwork/database/windows/downloads/utilsoft-087491.html

2.数据层项目的.net版本改成4.5以上,使用nuget安装 EntityFramework 6 +Oracle.ManagedDataAccess +Oracle.ManagedDataAccess.EntityFramework,都安装最新稳定版。

安装后app.config和web.config都会被加入如下配置项

<configSections>
 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/"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>
 <entityFramework>
 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
  <parameter value="mssqllocaldb" />
  </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.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>
 <runtime>
 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
  <publisherPolicy apply="no" />
  <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral" />
  </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>

注意 entityFramework和 system.data中的版本号,nuget安装后自动生成的一般没问题,我在安装之前把网上找的资料里的配置项放在里面了,但是版本号不一致,程序启动不了,一直没注意到版本号,

找了好一会才发现是这两个地方。

3.然后就可以添加实体模型了。此时如果vs中显示找不到与ef6 兼容的实体框架提供程序,需要将配置文件中的ef节的 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />删掉或者注释掉,保存后再重新尝试添加实体模型。

添加实体模型时需要先不选择数据库里的表,即生成空模型,然后打开edmx文件,在模型浏览器中选中实体模型,在属性中把DDL生成模板改成SSDLToOracle.tt (VS),数据库生成工作流改成Generate Oracle Via T4 (TPT).xaml (VS)。

这么做的原因是如果DDL生成模板使用默认项SSDLToOracle.tt ,oracle中的number(1,0)和number(2,0)类型的字段生成的实体属性的类型会是int16,然后运行的时候报映射不匹配的错误(错误代码2019)。

报错原因是oracle从ODP.NET 12.1.0.2开始为ef6采用新的默认类型映射,官网说明https://docs.oracle.com/cd/E56485_01/win.121/e55744/entityDataTypeMapping.htm#ODPNT8303,其中的 New Default Mappings 段。

SSDLToOracle.tt模板生成的属性的类型是number(1,0)对应boolean,number(2,0)对应byte,这个对应关系与新映射是一致的。

附上ef5的映射

Oracle Type Default EDM Type Custom EDM Type Number(1,0) Int16 bool Number(2,0) to Number(3,0) Int16 byte Number(4,0) Int16 Int16 Number(5,0) Int16 Int32 Number(6,0) to Number(9,0) Int32 Int32 Number(10,0) Int32 Int64 Number(11,0) to Number(18,0) Int64 Int64 Number(19,0) Int64 Decimal

总结

以上所述是小编给大家介绍的使用ef6创建oracle数据库的实体模型遇到的问题及解决方案,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

标签:
ef6,oracle,数据库实体模型

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。