使用Entity Framework Core访问数据库(Oracle篇)

 哇。。看看时间 真的很久很久没写博客了 将近一年了。

最近一直在忙各种家中事务和公司的新框架  终于抽出时间来更新一波了。

本篇主要讲一下关于Entity Framework Core访问oracle数据库的采坑。。

强调一下,本篇文章发布之前 关于Entity Framework Core访问oracle数据库的甲骨文官方dll还未正式发布。

不过我已经在项目中用起来了。。介意的兄弟可以先等等。。甲骨文说的是本年第三季度。。

 

环境

1.官方文档中支持的环境

首先我们来看看所谓的官方支持吧。

操作系统:

1. Windows x64
1.1Windows 8.1 (Pro and Enterprise Editions)
1.2Windows 10 x64 (Pro, Enterprise, and Education Editions)
1.3Windows Server 2012 R2 x64 (Standard, Datacenter, Essentials, and FoundationEditions)
1.4Windows Server 2016 x64 (Standard and Datacenter Editions)
2.Linux x64
2.1Oracle Linux 7
2.2Red Hat Enterprise Linux 7


.NET版本:
1.NET Core 2.1 或者更高
2.NET Framework 4.6.1 或者更高


· Entity Framework Core版本:
1.   2.1版本或者更高


依赖库:
1. ODP.NET Core 18.3或者更高
2.Microsoft.EntityFrameworkCore.Relational 2.1或者更高
3.Access to Oracle Database 11g Release 2 (11.2) 或者更高
 

 

正文

 

本篇将采取CodeFirst的形式来创建数据库。。

1.创建数据库

我们创建上下文与实体如下:

复制代码
    public class BloggingContext : DbContext     {         public DbSet<Blog> Blogs { get; set; }         public DbSet<Post> Posts { get; set; }          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)         {             optionsBuilder.UseOracle(@"SQL Contion", b => b.UseOracleSQLCompatibility("11"));         }          protected override void OnModelCreating(ModelBuilder modelBuilder)         {          }     }      public class Blog     {         public int BlogId { get; set; }         public string Url { get; set; }         //public int Rating { get; set; }        public List<Post> Posts { get; set; }     }      public class Post     {         public int PostId { get; set; }         public string Title { get; set; }         public string Content { get; set; }          public int BlogId { get; set; }         public Blog Blog { get; set; }     }
复制代码

这里我们先介绍第一个要注意的地方,UseOracle参数里面跟的UseOracleSQLCompatibility方法,里面参数传递的11,指的是oracle11g版本。如果你是12g版本 请传递12.

因为11g和12g的SQL语法有较多不同的地方,所以用这个来区分。

 

然后我们add一个版本 执行nuget命令如下:(PS:不懂如何使用codeFirst的请移步:Entity Framework Core 之数据库迁移)

Add-Migration BanBen1

然后将版本更新到数据库如下:

关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信