Oracle中使用Entity Framework 6.x Code-First方式开发
去年写过一篇EF的简单学习笔记,当时EF还不支持Oracle的Code-First开发模式,今天无意又看了下Oracle官网,发现EF6.X已经支持了,并且给出了二篇教程(英文版):
1.Using NuGet to Install and Configure Oracle Data Provider for .NET
2.Entity Framework Code First and Code First Migrations for Oracle Database
基本上照着做就行了,为了方便不愿意啃英文的朋友,把主要步骤"意译"了下:
环境: Visual Studio 2013 + .Net Framework 4.5.2
1. 使用NuGet安装、配置ODP.NET
a) 参考下图,创建一个Console Application的项目
项目名称随便吧,图中是NuGet
这是主程序入口
b) 打开Solution 视图
项目的References引用节点右击,选择Manage NuGet Packages...
参考下图,搜索Oracle,安装图中的二项:
安装过程中,会弹出License对话框,点击I Accept
安装成功后,这二项应该会自动打上绿勾
安装完成后,会自动打开readme.txt,地球人一般都不看这玩意儿
检查下项目的References,参考下图,应该看到自动添加了4个新的dll引用
再看下App.config
会自动添加以下内容:
下图这二个地方,是用来配置连接字符串的,记得修改
ok, ODP.Net安装配置完成
2.使用Code First模式开发
a) 先参考下图,修改连接字符串(本文用的是HR这个示例用户,大家可以根据实际情况修改)
打开Program.cs这个文件
换成下面这段代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Migrations;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations.History;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace EFCodeFirst
{
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<OracleDbContext>()); using (var ctx = new OracleDbContext())
{
var emp = new Employee
{
Name = "Tom",
HireDate = DateTime.Now
}; ctx.Employees.Add(emp);
ctx.SaveChanges(); var dept = new Department
{
Name = "Accounting",
ManagerId = emp.EmployeeId
}; ctx.Departments.Add(dept);
ctx.SaveChanges();
} Console.Write("Press any key to continue... ");
Console.ReadLine();
}
} public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public DateTime HireDate { get; set; }
//public string Location { get; set; }
} public class Department
{
public int DepartmentId { get; set; }
public string Name { get; set; }
[ForeignKey("Manager")]
public int ManagerId { get; set; }
public Employee Manager { get; set; }
} public class OracleDbContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("HR");
}
}
}
下面是主要的调用代码,演示了insert记录
下面是实体类的定义,完全是POJO对象,可以借助工具或纯手写.
下面是DbContext部分,相当于DAL层.注意:OnModelCreating,这里表示根据Model创建表时,默认将创建到HR这个Database Schema下
运行结果
打开Server Explorer面板
连接到Oracle
可以看到根据Model定义,自动生成了二张表(注意下表名,自动加了复数)
可以直接查看数据
可以看到,成功插入了2条数据
b) Model与数据库的迁移合并
数据实体模型的类定义,往往随着需求的变化而变化,如果增加或减少了属性,EF可以自动生成相应的db脚本,同步修改表结构
先参考下图,进入PM控制台
输入Enable-Migrations启用数据库迁移功能
然后将Employee的类定义,把原来注释掉的Location属性行,去掉注释(即:增加了Location属性)
回到PM控制台,输入Add-Migration First 生成相应的db修改脚本
最后输入Update-Database更新表结构
打开Server Explorer视图,查看下Employees表
可以发现,已经增加了新字段Location
Oracle中使用Entity Framework 6.x Code-First方式开发的更多相关文章
- 在Oracle中使用Entity Framework 6 CodeFirst
项目中需要将系统从SQLServer数据库迁移到Oracle上.由于原大部分数据访问操作都是通过包装了Entity Framework的统一访问入口实现的,所以需要研究Entity Framework ...
- Oracle中使用Entity Framework 6.x Code-First
Oracle中使用Entity Framework 6.x Code-First方式开发 去年写过一篇EF的简单学习笔记,当时EF还不支持Oracle的Code-First开发模式,今天无意又看了下O ...
- 如何使用ASP.NET Web API OData在Oracle中使用Entity Framework 6.x Code-First方式开发 OData V4 Service
环境: Visual Studio 2013 + .Net Framework 4.5.2 1.新建项目 2.安装OData,ODP.NET 安装的包: 下面是部分代码: using System; ...
- Entity Framework 6.x Code Frist For Oracle 实践与注意点
Entity Framework 6.x Code Frist For Oracle 实践与注意点 开发环境 Visual Studio.net 2015/2017 Oracle 11g/12c 数据 ...
- 如何在ASP.NET Core中应用Entity Framework
注:本文提到的代码示例下载地址> How to using Entity Framework DB first in ASP.NET Core 如何在ASP.NET Core中应用Entity ...
- Oracle官方版Entity Framework
千呼萬喚始出來! Oracle官方版Entity Framework問市,邁入開發新時代 自從我得了一種"不用LINQ就不會寫資料庫程式"的病,為了滿足工作上要搭配Oracle(雖 ...
- Entity Framework工具POCO Code First Generator的使用
在使用Entity Framework过程中,有时需要借助工具生成Code First的代码,而Entity Framework Reverse POCO Code First Generator是一 ...
- Entity Framework工具POCO Code First Generator的使用(参考链接:https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator)
在使用Entity Framework过程中,有时需要借助工具生成Code First的代码,而Entity Framework Reverse POCO Code First Generator是一 ...
- dotnet ef执行报错, VS 2019发布时配置项中的Entity Framework迁移项显示不出来
VS 2019发布时配置项中的Entity Framework迁移项显示不出来 dotnet ef dbcontext list --json “无法执行,因为找不到指定的命令或文件.可能的原因包括: ...
随机推荐
- 第一个WPF应用程序
WPF 全称为 Windows Presentation Foundation. 核心特性: WPF使用一种新的XAML(Extensible Application Markup Language) ...
- WPF学习之路(十二)控件(HeaderedContent控件)
GroupBox 用来组织多种控件的常见控件,因为是内容空间,只能直接包含一项,需要使用面板一类的中间空间. Header和Content可以是任意元素 <GroupBox> <Gr ...
- ios界面布局整理
1 UIView 1.1 创建自定义的UIView的xib文件 [1]设置 file's Owner的 Custom Class 中的class 执行自定义控件类 [2]设置当前UIView 的屏幕大 ...
- 让UNION与ORDER BY并存于SQL语句当中
在SQL语句中,UNION关键字多用来将并列的多组查询结果(表)合并成一个结果(表),简单实例如下: SELECT [Id],[Name],[Comment] FROM [Product1] UNIO ...
- Apache安装问题:configure: error: APR not found . Please read the documentation
Linux上安装Apache时,编译出现错误: checking for APR... no configure: error: APR not found . Please read the do ...
- Java并发工具类Semaphore应用实例
package com.thread.test.thread; import java.util.Random; import java.util.concurrent.*; /** * Semaph ...
- Android设置AlertDialog点击按钮对话框不关闭(转)
(转自:http://blog.csdn.net/winson_jason/article/details/8485524) 当我们在用到Android alertDialog创建对话框 的时候,我们 ...
- TestNG之注解的生命周期
有必要介绍一下TestNG注解的生命周期,先看一下官网支持的注解有 @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeGroups@AfterGro ...
- NOIP2000方格取数[DP]
题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0.如下图所示(见样例): A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 ...
- NOIP2014飞扬的小鸟[DP][WRONG]
坑人啊朴素的dp 75分 用了完全背包才是80分,结果普遍偏小 为什么啊啊啊啊啊 等以后再写一遍吧 #include<iostream> #include<cstdio> #i ...