足球系统平台出租使用ef6创建oracle数据库的实体模型遇到的问题及解决方案

足球系统平台出租Q:157015557,解决方案中的数据层项目最初使用的是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/?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>
<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,这个对应关系与新映射是一致的。


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数据库的实体模型遇到的问题及解决方案,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

猜你喜欢

转载自blog.csdn.net/qq_42575115/article/details/80949227