zoukankan      html  css  js  c++  java
  • Oracle 与 entity framework 6 的配置,文档

    官方文档: 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=&quot;DATA SOURCE=192.168.100.18:1521/gdaa;PASSWORD=gdaa;PERSIST SECURITY INFO=True;USER ID=GDCD&quot;" />
      
                <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  有类似的指导。

                       http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx

        

               其他 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

               http://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Internal/LazyInternalContext.cs

                   internal class LazyInternalConnection : InternalConnection 源代码来源:

                http://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Internal/LazyInternalConnection.cs

         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();

            }

  • 相关阅读:
    点击鼠标后系统自动生成对应消息
    mfc 鼠标、键盘响应事件
    VC中键盘键的对应关系
    补充知识及数据类型
    Python入门
    tomcat启动报错
    正则表达式
    MySQL修改root密码的方法
    mysql 压缩包免安装版 安转步骤
    springmvc--json--返回json的日期格式问题
  • 原文地址:https://www.cnblogs.com/hopesun/p/4388641.html
Copyright © 2011-2022 走看看