官方文檔: http://docs.oracle.com/cd/E56485_01/win.121/e55744/intro001.htm#ODPNT123
Oracle 對 微軟 實體框架 EF6 的支持,在 ODP.NET 的新版本中才有實現。
Oracle Data Access Components (ODAC) Windows 下載: ODAC 12c Release 3
包括支持 Entity Framework 6 Code First and Code First Migrations; NuGet 包,
.NET Framework 4.5.2; and ODP.NET, Managed Driver XML DB.
http://www.oracle.com/technetwork/topics/dotnet/downloads/index.html
第一部分: 使用 VS 的菜單進行配置。
實踐很多次, 成功建立 實體數據集: 必須在 VS2013 UPDATE4 安裝完成后,安裝
1) 32-bit ODAC with Oracle Developer Tools for Visual Studio Downloads
2) 在建立 VS 解決方案后, 安裝 ORACLE 的 NuGet 包 (兩個包)。 --- 通過 包管理器。
這樣才能 在解決方案的項目配置 文件 APP.config 或者 WEB.config 中創建一些配置信息!!!
<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
完成上述兩步驟,就可以 新增: ADO。NET 實體數據模型,來引用 ORACLE 數據庫。
依據 是EF 設計器 或者 Code First 代碼優先,會在 APP.config 或者 WEB.config 產生一個 連接字符串
<add name="CDEntities" providerName="System.Data.EntityClient" connectionString="metadata=res://*/Model.CDDBModel.csdl|res://*/Model.CDDBModel.ssdl|res://*/Model.CDDBModel.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="DATA SOURCE=192.168.100.18:1521/gdaa;PASSWORD=gdaa;PERSIST SECURITY INFO=True;USER ID=GDCD"" />
<add name="HPModel" providerName="Oracle.ManagedDataAccess.Client" connectionString="DATA SOURCE=192.168.100.18:1521/gdaa;PASSWORD=gdaa;PERSIST SECURITY INFO=True;USER ID=GDAA"/></connectionStrings>
特別的問題是:如果是ASPNET MVC,將數據庫模型Model 單獨作為一個:DLL 類庫 項目, VC 作為一個MVC 項目,則創建過程有一定要求:
1) 首先創建 DLL 類庫 項目 解決方案,增加 ORACLE 的專用NUGET 包,然后 新增: ADO.NET 實體數據模型;
2) 再創建一個 ASPNET MVC 解決方案,增加 ORACLE 的專用NUGET 包;
3) 將 數據模型方案的項目 引入 MVC 方案中, 復制 數據模型方案的 APP.CONFIG 中鏈接連接字符串 <add name="XXXXX" ......,增加到 MVC方案的WEB.CONFIG 中.
主要是由於,數據模型上下文 DBCONTEXT 在 MVC 代碼中實例化時,獲取 鏈接連接字符串 必須是當前 方案的配置文件。
============================================================================
第二部分 如何使用代碼 進行連接。
上述都是通過 配置文件 APP.config 或者 WEB.config 的連接字符串connectionString 。實體上下文 DbContext 繼承類 的構造函數,傳入 “鏈接字符名稱” ,使用 :Base("name =connectionString") 的方式 ,這種方式是利用 IIS 啟動或者實例化時,通過 讀取配置文件的連接信息。
下面重點收集,如何通過代碼進行 實體上下文 DbContext 的實例化,即需要使用 XXDbContext db = new XXDbContext(connstring) 時 ,如何傳入連接字符串?
1)官方網站 EF 有提到:https://msdn.microsoft.com/en-US/data/ef 源代碼網: http://entityframework.codeplex.com/
此處的最后有專門談到如何通過代碼進行連接, https://msdn.microsoft.com/en-us/data/jj592674
比較老的BLOG Using DbContext in EF 4.1 Part 2: Connections and Models 有類似的指導。
其他 DbContext 構造函數選項:DbContext 類包含支持其他一些更高級方案的其他構造函數和使用模式。其中一些選項有:
- 可以使用 DbModelBuilder 類構建 Code First 模型,而不實例化 DbContext 實例。這樣會生成 DbModel 對象。隨后,在准備好創建 DbContext 實例時,可以將此 DbModel 對象傳遞給某一 DbContext 構造函數。
- 可以將完整連接字符串傳遞給 DbContext,而不僅僅傳遞數據庫或連接字符串名稱。此連接字符串默認用於 System.Data.SqlClient 提供程序;可以通過在 context.Database.DefaultConnectionFactory 上設置不同的 IConnectionFactory 實現來更改此行為。
特別說明:官方文檔顯示,此屬性已過時,從系統中實例化的上下文對象中也不能找到此!!! https://msdn.microsoft.com/zh-cn/library/system.data.entity.database.defaultconnectionfactory(v=vs.113).aspx 需要尋找其它改進的方式。
- 可以通過將現有 DbConnection 對象傳遞給 DbContext 構造函數來使用該對象。如果連接對象是 EntityConnection 的實例,則將使用連接中指定的模型,而不使用 Code First 計算模型。如果該對象是其他某一類型(例如 SqlConnection)的實例,則上下文將在 Code First 模式下使用該對象。
============================================
第三部分:DBCONTEXT 的構造方法之一 ,使用連接字符串進行初始化。
DbContext 源代碼來源於: http://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/DbContext.cs
LazyInternalContext 源代碼來源:繼承於 internal class LazyInternalContext : InternalContext
internal class LazyInternalConnection : InternalConnection 源代碼來源:
public DbContext(string nameOrConnectionString) {
Check.NotEmpty(nameOrConnectionString, "nameOrConnectionString");
InitializeLazyInternalContext(new LazyInternalConnection(this, nameOrConnectionString));
}
internal virtual void InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model = null) {
DbConfigurationManager.Instance.EnsureLoadedForContext(GetType());
_internalContext = new LazyInternalContext( this, internalConnection, model ,
DbConfiguration.DependencyResolver.GetService<Func<DbContext, IDbModelCacheKey>>() ,
DbConfiguration.DependencyResolver.GetService<AttributeProvider>() );
DiscoverAndInitializeSets();
}