使用Entity Framework Core訪問數據庫(Oracle篇) Entity Framework Core 之數據庫遷移

前言

哇。。看看時間 真的很久很久沒寫博客了 將近一年了。

最近一直在忙各種家中事務和公司的新框架  終於抽出時間來更新一波了。

本篇主要講一下關於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還大。。我們下篇在寫。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章