Oracle数据库用EF操作的示例

Using EF Oracle Sample Provider with EDM Designer  (from msdn)

Many people are asking if it is possible to use EFOracleProvider with EDM Designer in Visual Studio 2008 SP1. The answer is yes, but because the sample doesn‘t include a DDEX provider required for VS integration, there are certain steps that have to be run manually.

I‘ve compiled a step-by-step guide for those interested in trying this out (this assumes NorthwindEF sample database installed according to instructions included with the sample, but it should be straightforward to adjust it to your own setup)

PART 1 : INSTALLING ORACLE SAMPLE PROVIDER

1. Download and unzip EFOracleSampleProvider.zip from http://code.msdn.com/EFOracleProvider

2. Follow instructions in the README.txt to set up a sample database.

3. Open elevated Visual Studio instance. Build the sample project.

4. Open elevated command prompt and open machine.config using notepad:

notepad %WINDIR%\Microsoft.NET\Framework\v2.0.50727\config\machine.config

5. Find <DbProviderFactories> section and add EFOracleProvider entry:

<add name="EF Oracle Data Provider" invariant="EFOracleProvider"
     description="EF Provider for Oracle testing"
     type="EFOracleProvider.EFOracleProviderFactory, EFOracleProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b"/>

6. The completed system.data section has to look similar to this:

<system.data>
  <DbProviderFactories>
    <add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Provider for Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Provider for OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add name="EF Oracle Data Provider" invariant="EFOracleProvider" description="EF Provider for Oracle testing" type="EFOracleProvider.EFOracleProviderFactory, EFOracleProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b"/>
    <add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
  </DbProviderFactories>
</system.data>

PART 2: GENERATING MODEL FROM ORACLE DATABASE

7. Create a new project in VS. For simplicity let‘s create a console application

8. Open elevated command prompt. Enter the directory that contains the newly created project and run the following command:

%WINDIR%\Microsoft.NET\Framework\v3.5\edmgen.exe /provider:EFOracleProvider /mode:fullgeneration        /connectionstring:"data source=XE;user id=edmuser;password=123456" /project:NorthwindEFModel

The output should be:

Microsoft (R) EdmGen version 3.5.0.0
Copyright (C) 2008 Microsoft Corporation. All rights reserved.

Loading database information...
warning 6005: The data type ‘timestamp(9)‘ is not supported, the column ‘OrderDate‘ in table ‘dbo.Orders‘ was excluded.
warning 6005: The data type ‘timestamp(3)‘ is not supported, the column ‘RequiredDate‘ in table ‘dbo.Orders‘ was excluded.
Writing ssdl file...
Creating conceptual layer from storage layer...
Writing msl file...
Writing csdl file...
Writing object layer file...
Writing views file...

Generation Complete -- 0 errors, 2 warnings

9. This will create a bunch of NorthwindEFModel.* files for you.

10. Open Northwind.ssdl file in a text editor and replace all instances of Schema="dbo" with empty string (this is needed because tables in the sample Oracle database don‘t use a schema)

11. In order to use generated model in the EF Ddesigner, we have to create NorthwindEFModel.edmx file. This can be done manually (just copy/paste contents of individual files into an empty EDMX as indicated by the comments) or by using EdmGen2 tool from Code Gallery:

C:\Path\To\EdmGen2.exe /toedmx NorthwindEFModel.csdl NorthwindEFModel.ssdl NorthwindEFModel.msl

12. This will create NorthwindEFModel.edmx, which we can add to the project in VS.

13. At this point you can now delete the following files generated by EdmGen.exe, which won‘t be necessary:

  • NorthwindEFModel.csdl
  • NorthwindEFModel.ssdl
  • NorthwindEFModel.msl
  • NorthwindEFModel.ObjectLayer.cs
  • NorthwindEFModel.Views.cs

PART 3: TESTING GENERATED MODEL

14. The only remaining thing to do is to add App.config file with connection string for our Oracle database:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="NorthwindEFModelContext"
         connectionString="provider=EFOracleProvider;
                           metadata=res://*/NorthwindEFModel.csdl|res://*/NorthwindEFModel.ssdl|res://*/NorthwindEFModel.msl;
                           Provider Connection String=‘data source=XE;user id=edmuser;password=123456‘"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

15. We can now try out our model by running a sample LINQ to Entities query:

using (NorthwindEFModelContext context = new NorthwindEFModelContext())
{
    foreach (var c in context.Customers.Where(c=>c.City == "Seattle"))
    {
        Console.WriteLine(c.CompanyName);
    }
}

本人用我的poor英文水平,简单的翻译了一下(供参考):

许多人问它是否可能与 EDM 设计器在 Visual Studio 2008 SP1 中使用EFOracleProvider 。答案是肯定的但因为该示例不包括 VS 集成所需的 DDEX 提供程序,有某些必须手动运行的步骤。

我总结了一个分步指南,对于那些有兴趣尝试这 (这是假设 NorthwindEF 示例数据库根据示例,附带的说明,安装,但它应该很简单,调整到您自己的安装程序)

第 1 部分: 安装 ORACLE 示例提供程序

1.下载并解压缩 EFOracleSampleProvider.zip 从http://code.msdn.com/EFOracleProvider

2.按照说明在 README.txt 中的设置一个示例数据库。

3.开放提升的 Visual Studio 实例。生成示例项目。

4.打开提升的命令提示符并 machine.config 使用记事本打开:
notepad %WINDIR%\Microsoft.NET\Framework\v2.0.50727\config\machine.config

5.找到 <DbProviderFactories> 部分并添加 EFOracleProvider 条目:

<add name="EF Oracle Data Provider" invariant="EFOracleProvider"
     description="EF Provider for Oracle testing"
     type="EFOracleProvider.EFOracleProviderFactory, EFOracleProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b"/>

6.已完成的 system.data 部分有看上去类似这样:

<system.data>
  <DbProviderFactories>
    <add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Provider for Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Provider for OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add name="EF Oracle Data Provider" invariant="EFOracleProvider" description="EF Provider for Oracle testing" type="EFOracleProvider.EFOracleProviderFactory, EFOracleProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b"/>
    <add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
  </DbProviderFactories>
</system.data>

第 2 部分: 从 ORACLE 数据库生成模型

7.在 VS 中创建一个新的项目。为简单起见,让我们创建一个控制台应用程序

8.打开提升的命令提示符。输入包含新创建的项目的目录,然后运行以下命令:

%WINDIR%\Microsoft.NET\Framework\v3.5\edmgen.exe /provider:EFOracleProvider /mode:fullgeneration        /connectionstring:"data source=XE;user id=edmuser;password=123456" /project:NorthwindEFModel

输出应该是:

Microsoft (R) EdmGen version 3.5.0.0
Copyright (C) 2008 Microsoft Corporation. All rights reserved.

Loading database information...
warning 6005: The data type ‘timestamp(9)‘ is not supported, the column ‘OrderDate‘ in table ‘dbo.Orders‘ was excluded.
warning 6005: The data type ‘timestamp(3)‘ is not supported, the column ‘RequiredDate‘ in table ‘dbo.Orders‘ was excluded.
Writing ssdl file...
Creating conceptual layer from storage layer...
Writing msl file...
Writing csdl file...
Writing object layer file...
Writing views file...

Generation Complete -- 0 errors, 2 warnings

9.这将为您创建 NorthwindEFModel.* 文件一的束。

10.在文本编辑器中打开 Northwind.ssdl 文件并替换所有实例的架构 ="dbo"与空字符串 (这需要,因为示例 Oracle 数据库中的表不使用架构)

11.对在 EF Ddesigner 中使用生成的模型,我们必须创建 NorthwindEFModel.edmx 文件。这可以手动完成 (只复制/粘贴内容成空 EDMX 如评论所示的单个文件) 或使用EdmGen2 工具从代码库:

C:\Path\To\EdmGen2.exe /toedmx NorthwindEFModel.csdl NorthwindEFModel.ssdl NorthwindEFModel.msl

12.这将创建 NorthwindEFModel.edmx,我们可以添加到项目中,VS。

13.在这一点,你现在可以删除生成 EdmGen.exe,这不会是必需的以下文件:

  • NorthwindEFModel.csdl
  • NorthwindEFModel.ssdl
  • NorthwindEFModel.msl
  • NorthwindEFModel.ObjectLayer.cs
  • NorthwindEFModel.Views.cs

第 3 部分: 测试生成的模型

14.剩下的唯一要做是我们的 Oracle 数据库与连接字符串 App.config 文件中添加:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="NorthwindEFModelContext"
         connectionString="provider=EFOracleProvider;
                           metadata=res://*/NorthwindEFModel.csdl|res://*/NorthwindEFModel.ssdl|res://*/NorthwindEFModel.msl;
                           Provider Connection String=‘data source=XE;user id=edmuser;password=123456‘"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

15.我们现在可以尝试我们的模型通过对实体查询运行 LINQ 示例:

using (NorthwindEFModelContext context = new NorthwindEFModelContext())
{
    foreach (var c in context.Customers.Where(c=>c.City == "Seattle"))
    {
        Console.WriteLine(c.CompanyName);
    }
}
 

Oracle数据库用EF操作的示例

时间: 2024-10-13 07:55:59

Oracle数据库用EF操作的示例的相关文章

Oracle数据库安装图文操作步骤

Oracle数据库安装图文操作步骤 一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地址: http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html以下两网址来源此官方下载页网. win 32位操作系统 下载地址: http://download.oracle.com/otn

混合框架中Oracle数据库的还原处理操作

在较早期的随笔<Oracle如何实现创建数据库.备份数据库及数据导出导入的一条龙操作>粗略介绍了Oracle数据库的备份还原操作,本文想从开发框架的基础上介绍Oracle数据库的脚本或者还原操作. 我们在Winform开发框架.混合式开发框架.Web开发框架等各种开发框架,底层都是支持多种数据库的,如MS SQLServer.Oracle.MySQL.SQLite.PostgreSQL等等,如下图所示. 其中SQLServer支持最为常用,而且也是最为方便备份还原的操作,而Oracle数据库这

oracle数据库升级dbua操作阻塞解决方法(解决ORA-32004报错)

操作环境 1.SuSE11sp3操作系统 2.oracle 11.2.0.3版本升级到11.2.0.4版本 问题现象   oracle 11.2.0.3版本升级到11.2.0.4版本时执行dbua命令在获取dabase信息步骤时提示输入pfile文件,无法next操作,升级阻塞.(不是在自己环境操作,暂无截图) 问题分析   oracle start mount步骤时会出现提示ORA-32004: obsolete or deprecated parameter(s) specified for

jdbc执行Oracle数据库(oracle10g)Insert操作时 日期类型时间部分丢失的问题.

用ibatis执行一个Insert语句时,日期类型时间部分丢失,对应的java类型是java.util.Date.数据库对应的类型是DATE 解决办法:把java.util.Date转换成java.sql.TimeStamp 时间戳类型..

oracle 数据库的一些操作

1.查看实例名:show parameter instance_name 2.查看数据库归档是否开启:archive log list 3.查看spfile参数:show parameter spfile 4.查看日志文件位置:select group#,member from v$logfile 5.查看归档文件:show parameter recovery 6.查看告警日志文件(位于bdump路径下以alert开头的文件):show parameter dump 7.查看监听状态命令:ls

oracle数据库产生误操作,将一个字段置空了,恢复数据

工作的时候没仔细检查sql直接执行了,将线上的数据弄错了,发现抓紧修改,还好只修改了一个表的一列: 在网上查了下oracle可以查询24小时任何时刻的数据 select * from btgl_ylbt_sqb t as of timestamp to_timestamp('2018-01-24 10:56:00','yyyy-mm-dd hh24:mi:ss'); --查询到此时数据 --将这列恢复到这个时刻 update btgl_ylbt_sqb sqb   set sqb.duratio

在linux环境上对oracle数据库的数据泵导入导出操作

需求:公司搭建的报表库环境数据来自于生产环境oracle数据,因此需要使用kettle将oracle数据每天定时导入到报表数据库中(本文不讨论如何用kettle导数据,不赘述),若要导数据我们需要一套完善的生产oracle数据库的表结构.同时环境搭建以及oracle数据库都在linux环境下部署,因此,需要在linux环境下使用数据泵技术将数据完成导入导出 以上为需求概述 具体实现(即:操作步骤): su - root --切换root用户(由于不知道oracle用户的密码使用root来登录or

Oracle数据库类型

字符类型char[(length)] 定长字符 最长2000字节varchar2[(length)] 可变长度的字符数据类型,最长4000字节NCHAR[(length)] 固定长度的字符数据类型,由国际字符语言(national language Support,NLS)字符集字符组成,最长2000字节LONG 可变长度的字符数据类型 最长2GBRAW 原始的二进制数据类型,最长2000字节long raw 原始的二进制数据类型,最长2GBrawid 数据库中一窜12字节地址,用于定位磁盘上某

Oracle数据库之创建和删除数据库

创建数据库 1 使用Database Configuration  Assistant工具创建Oracle数据库 步骤一 操作窗口 有4种选择  A 创建数据库 B 配置数据库选件 C 删除数据库 D 管理模板 步骤二 数据库模板 窗口 有3种选择  A 一般用途或事务处理 B 定制数据库 C 数据仓库 步骤三 数据库标识 在这一步中,需要输入 全局数据库名 和 Oracle 系统标识符(SID) 全局数据名是Oracle 数据库的唯一标识,所以不能与已有的数据库重名 打开oracle数据库时,