Entity Framework 6, database-first with Oracle
Entity Framework 6, database-first with Oracle
转载自http://csharp.today/entity-framework-6-database-first-with-oracle/

I spent a lot of time trying to find out how to implement Entity Framework 6 database-first scenario with Oracle database. It’s not as straightforward as you might think. I searched various websites and found only confusing information. Finally I got it working, therefore I can confirm that EF6 database-first works with Oracle databases.
First, let me clarify my environment. I have Visual Studio 2013 and .NET Framework 4.5.2 installed.
Secondly, a word about my goal. I wanted to create a MVC5 website that connects to existing Oracle database.
So, here is how I did it.
Setup Oracle Developer Tools
First I had to install Oracle Data Access Components (ODAC) with Oracle Developer Tools for Visual Studio. Yep, long name. You can get it from Oracle web site (you’ll have to register). Make sure to take ODAC 12c Release 3 as previous versions don’t support Entity Framework 6. You will get it in a form of zip file. Next extract it and run setup.exe (it’s java so will take some time). Standard installation worked for me – just click Next couple of times and Install. If you want to alter the installation, make sure you:
- Note Software location – the default is C:\app\client\USERNAME\product\12.1.0\client_1
- Have Oracle Developer Tools for Visual Studio component selected – that’s what we are after :)
- Select Visual Studio version that will be configured with the developer tools – you can select few, I needed only VS 2013
- Check Configure ODP.NET at a machine-wide level – it will install it in GAC
Install Entity Framework 6
I wanted to use EF6 in my MVC5 web project. A new MVC5 project has already reference to EF6, so there was nothing to do. But if you have different project you might have to install Entity Framework 6 manually. The easiest way is to use NuGet packages:
- NuGet manager is included in VS 2013, but if you use VS 2010 you will have to install it:
- Open VS and click Tools menu, then Extensions and Updates
- Click Online on the left hand panel, then type NuGet in search text box on the right side
- It should find NuGet Package Manager – click on it and press Download button
- Visual Studio restart will be required
- Next, open you project, right click it and choose Manage NuGet Packages from context menu
![]()
- Click Online on the left side, then type Entity in search box
- Click EntityFramework and press Install
![]()
Reference Oracle libraries
To leverage Entity Framework capabilities you have to add reference to Oracle Data Access library.
- Right click on References, then Add reference… in context menu
![]()
- Click Browse button and find following library:
C:\app\client\USERNAME\product\12.1.0\client_1\odp.net\managed\common\Oracle.ManagedDataAccess.dll
(location might be different if you changed it during ODAC installation) - Browse and find one more library:
C:\app\client\USERNAME\product\12.1.0\client_1\odp.net\managed\common\EF6\Oracle.ManagedDataAccess.EntityFramework.dll
![]()
Add Oracle provider for Entity Framework 6
This is quite important step. If you don’t do it you will see following error when attempting to generate model from database. I lost a lot of time trying to resolve it :|
![]()
To add Oracle provider you need to open web.config and add following:
<configuration>
<entityFramework>
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
It was enough to fix it for me, but seen a lot of comments that following is required as well. You can check if it’s needed in your case.
<configuration>
<configSections>
<section name="Oracle.ManagedDataAccess.Client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
Generate Entity Data Model from database schema
Finally, it’s time to do the actual work.
- Add new item to the project
- Choose Visual C# then Data and select ADO.NET Entity Data Model
![]()
- Select Generate from database and press Next
- Select connection or add new connection – I had some issues when tried to add new connection (see the solution at the bottom)
- Choose how to store sensitive data
![]()
- Choose database object that should be included in the model
![]()
That’s it. Well done!
Summary
Configuring Entity Framework 6 to work with Oracle database in database-first scenario isn’t as easy as one would expect. Fortunately it’s doable.
I also tried more elegant solution – to use ODAC NuGet package. But lost a lot of time and finally couldn’t make it work. Then found following comment on Oracle web site – meaning ODAC Release 3 which has EF6 support is not uploaded as NuGet package yet.
Note: NuGet installation is not currently available, but will be available shortly.
Issue with adding new Oracle connection
I had a strange issue when I tried to setup database connection. I clicked New connection and couldn’t find my database in Data source list, so I clicked Advanced button.
![]()
I filled following fields and clicked OK.
- Security / User Id
- Security / Password
- Source / Data Source
Then clicked Test Connection and was again surprised – it couldn’t find my instance. I checked some SQL client and my database was working well. I did some searching and found following way to overcome this impairment.
- Run tnsping INSTANCE_NAME command to get details about database instance
- Go again to Advanced window and in Data Source type something like: server:port/INSTANCE.WORLD
- This time test connection will succeed.
![]()
Interesting is that it’s not able to connect only in wizard mode. Later I updated data source in web.config (fromserver:port/INSTANCE.WORLD back to only INSTANCE) and application worked fine.
Entity Framework 6, database-first with Oracle的更多相关文章
- 《ASP.NET MVC4 WEB编程》学习笔记------Entity Framework的Database First、Model First和Code Only三种开发模式
作者:张博出处:http://yilin.cnblogs.com Entity Framework支持Database First.Model First和Code Only三种开发模式,各模式的开发 ...
- Entity Framework 之Database first(数据库优先)&Model First(模型优先)
一.什么是Entity Framework 1.1 实体框架(EF)是一个对象关系映射器,使.NET开发人员使用特定于域的对象与关系数据.它消除了需要开发人员通常需要编写的大部分数据访问代码.简化了原 ...
- C# ORM—Entity Framework 之Database first(数据库优先)&Model First(模型优先)(一)
一.什么是Entity Framework 1.1 实体框架(EF)是一个对象关系映射器,使.NET开发人员使用特定于域的对象与关系数据.它消除了需要开发人员通常需要编写的大部分数据访问代码.简化了原 ...
- 使用Entity Framework Core访问数据库(Oracle篇)
前言 哇..看看时间 真的很久很久没写博客了 将近一年了. 最近一直在忙各种家中事务和公司的新框架 终于抽出时间来更新一波了. 本篇主要讲一下关于Entity Framework Core访问ora ...
- 转:Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数
public IEnumerable<Statistic> GetStatistics(IEnumerable<Guid> itemIds) { var ctx = new D ...
- [转]Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数
本文转自:http://www.cnblogs.com/xchit/p/3334782.html 目前,EF对存储过程的支持并不完善.存在以下问题: EF不支持存储过程返回多表联合查询的 ...
- Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数
目前,EF对存储过程的支持并不完善.存在以下问题: EF不支持存储过程返回多表联合查询的结果集. EF仅支持返回返回某个表的全部字段,以便转换成对应的实体.无法支持返回部分字段的情况. 虽然可以正常导 ...
- Entity Framework 利用 Database.SqlQuery<T> 执行存储过程,并返回Output参数值
做个记录: var pCount = this._dataProvider.GetParameter(); pCount.ParameterName = "totalCount"; ...
- 在Oracle中使用Entity Framework 6 CodeFirst
项目中需要将系统从SQLServer数据库迁移到Oracle上.由于原大部分数据访问操作都是通过包装了Entity Framework的统一访问入口实现的,所以需要研究Entity Framework ...
随机推荐
- NodeJs解析web一例
var http = require('http'); var fs = require('fs'); var url = require('url'); http.createServer(func ...
- jenkins发送带附件(logfile.log和index.html)的邮件配置
先进入到job里面,在Attachment中按照规矩添加文件就好了 此处是以workspace作为根目录的,logfile.log文件刚好就在根目录上,所以直接写上,多个文件的话用逗号分隔, 第二个文 ...
- Python实现插件机制——自动import一个目录下的所有.py文件
假设有这样一个目录结构: /src main.py /plugins __init__.py a.py ...
- Linux 驱动学习笔记05--字符驱动实例,实现一个共享内存设备的驱动
断断续续学驱动,好不容易有空,做了段字符驱动的例子.主要还是跟书上学习在此记录下来,以后说不定能回过头来温故知新. 首先上驱动源码 gmem.c: /************************* ...
- c#窗体去掉borderstyle进行拖动
private void BaseCForm_Load(object sender, EventArgs e) { DrawStyle(); this.panel1 ...
- python之路-Day4
本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 函数是什么? 函数一词来源于数学,但编程中的「 ...
- 源码编译Nodejs 4.6 on CentOS6
1.本来是要按照个newman程序,无奈newman必须要node版本>=4,所以不得不安装node4.6 下载地址:https://nodejs.org/dist/v4.6.0/node-v4 ...
- Xcode导航栏不显示模拟器选择框ToolBar
不显示ToolBar的小伙伴可能就是下面的样子: 全屏后就可以看到ToolBar,像下面这样: 刚开始还以为是模拟器没装,还傻不拉几的去下载模拟器,后来才发现,只要下面的操作即可显示 点击" ...
- C# Winform学习--- 实现石头剪刀布的游戏
本文使用winform实现简单的石头剪刀布的游戏,主要实现,电脑随机出拳,玩家手动点击出拳:实现简易背景图片3秒切换:简易统计信息. 1.效果图 2.实现代码 新建一个windows窗体程序,用数字1 ...
- 把vim当做golang的IDE
开始决定丢弃鼠标,所以准备用vim了. 那么在vim里面如何搭建golang环境呢? git盛行之下,搭建vim环境是如此简单. 而且vim搭建好了之后,基本上跟IDE没有差别. 高亮.自动补全.自动 ...