前言
哇.. 看看时间 真的很久很久没写博客了 将近一年了.
最近一直在忙各种家中事务和公司的新框架 终于抽出时间来更新一波了.
本篇主要讲一下关于 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
然后将版本更新到数据库如下:
Update-Database
数据库生成成功.
2. 关于 oracle 序列的坑
我们这时候编写插入语句如下:
- using (BloggingContext db = new BloggingContext())
- {
- db.Blogs.Add(new Blog { Url = "aaaaa1" });
- db.SaveChanges();
- }
看似没问题的语句, 会得到一个错误消息如下:
- Index was out of range. Must be non-negative and Less than the size of the collection.
- Parameter name: index
这是因为我们没有给主键赋值导致的错误信息.(因为 oracle 没有自增主键, 只能通过序列自增)
那么自增序列如何使用呢?
我们查看数据库会发现, 如图:
codefirst 已经帮我们生成了序列, 但是并不会自动使用. 我们需要配置一下:
在上下文中的 OnModelCreating 方法添加如下代码:
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- modelBuilder.Entity<Post>(entity =>
- {
- entity.ToTable("Posts");
- entity.Property(o => o.PostId).ForOracleUseSequenceHiLo("Posts_PostId_sq3");
- });
- modelBuilder.Entity<Blog>(entity =>
- {
- entity.ToTable("Blogs");
- entity.Property(o => o.BlogId).ForOracleUseSequenceHiLo("Blogs_BlogId_sq1");
- });
- }
指定对应表的序列.
然后在运行. 即可添加成功了.
3. 关于在 Docker 中部署的坑
在我的生产项目中. 应该是打包到 docker 直接运行部署的.
不过在打包到 docker 的过程中又出现了诡异的问题.
就不重现了.. 反正就是开发环境没有问题.. 直接放到 Linux 中也没问题. 但是一旦打包到 docker 运行 就会查询不到数据.
经过多方查证 最终发现是微软提供的 rumtime 镜像, 因为是精简版系统 所以里面的市区有问题.
在 dockerfile 中添加如下语句 在生成的时候 设置好时区:
- FROM microsoft/dotnet:2.1-aspnetcore-runtime
- ENV TZ=Asia/Shanghai
这样就能成功的操作到数据库了..
结束语
近期移植了好些个项目到. NET CORE 或多或少遇到了不少坑.. 应该算是采坑无数了..
其实大部分都集中在数据库连接这一块.. 比如 oracle DB2 ..(PS: 感觉也就 MySQL 与 sql server 支持是最好的..)
DB2 虽然官方发布了. 但是他的坑其实比 oracle 还大.. 我们下篇在写..
来源: https://www.cnblogs.com/GuZhenYin/p/10756548.html