解決方案中的數據層項目最初使用的是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,這個對應關系與新映射是一致的。
附上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數據庫的實體模型遇到的問題及解決方案,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com