使用Entity Framework Core访问数据库(Oracle篇)
前言
哇。。看看時間 真的很久很久沒寫博客了 將近一年了。
最近一直在忙各種家中事務(wù)和公司的新框架 ?終于抽出時間來更新一波了。
本篇主要講一下關(guān)于Entity Framework Core訪問oracle數(shù)據(jù)庫的采坑。。
強(qiáng)調(diào)一下,本篇文章發(fā)布之前 關(guān)于Entity Framework Core訪問oracle數(shù)據(jù)庫的甲骨文官方dll還未正式發(fā)布。
不過我已經(jīng)在項(xiàng)目中用起來了。。介意的兄弟可以先等等。。甲骨文說的是本年第三季度。。
?
環(huán)境
1.官方文檔中支持的環(huán)境
首先我們來看看所謂的官方支持吧。
操作系統(tǒng):
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的形式來創(chuàng)建數(shù)據(jù)庫。。
1.創(chuàng)建數(shù)據(jù)庫
我們創(chuàng)建上下文與實(shí)體如下:
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參數(shù)里面跟的UseOracleSQLCompatibility方法,里面參數(shù)傳遞的11,指的是oracle11g版本。如果你是12g版本 請傳遞12.
因?yàn)?1g和12g的SQL語法有較多不同的地方,所以用這個來區(qū)分。
?
然后我們add一個版本 執(zhí)行nuget命令如下:(PS:不懂如何使用codeFirst的請移步:Entity Framework Core 之?dāng)?shù)據(jù)庫遷移)
Add-Migration BanBen1然后將版本更新到數(shù)據(jù)庫如下:
Update-Database數(shù)據(jù)庫生成成功。
?
2.關(guān)于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
這是因?yàn)槲覀儧]有給主鍵賦值導(dǎo)致的錯誤信息。(因?yàn)閛racle沒有自增主鍵,只能通過序列自增)
那么自增序列如何使用呢?
我們查看數(shù)據(jù)庫會發(fā)現(xiàn),如圖:
codefirst已經(jīng)幫我們生成了序列,但是并不會自動使用。我們需要配置一下:
在上下文中的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");
});
}
指定對應(yīng)表的序列。
然后在運(yùn)行。即可添加成功了。
?
3.關(guān)于在Docker中部署的坑
在我的生產(chǎn)項(xiàng)目中。應(yīng)該是打包到docker直接運(yùn)行部署的。
不過在打包到docker的過程中又出現(xiàn)了詭異的問題。
就不重現(xiàn)了。。反正就是開發(fā)環(huán)境沒有問題。。直接放到linux中也沒問題。但是一旦打包到docker運(yùn)行 就會查詢不到數(shù)據(jù)。
經(jīng)過多方查證 最終發(fā)現(xiàn)是微軟提供的rumtime鏡像,因?yàn)槭蔷啺嫦到y(tǒng) 所以里面的市區(qū)有問題。
在dockerfile中添加如下語句 在生成的時候 設(shè)置好時區(qū):
FROM microsoft/dotnet:2.1-aspnetcore-runtimeENV TZ=Asia/Shanghai
這樣就能成功的操作到數(shù)據(jù)庫了。。
?
結(jié)束語
近期移植了好些個項(xiàng)目到.NET CORE 或多或少遇到了不少坑。。應(yīng)該算是采坑無數(shù)了。。
其實(shí)大部分都集中在數(shù)據(jù)庫連接這一塊。。比如oracle ?DB2 。。(PS:感覺也就mysql與sql server支持是最好的。。)
DB2雖然官方發(fā)布了。但是他的坑其實(shí)比oracle還大。。我們下篇在寫。。
原文地址:https://www.cnblogs.com/GuZhenYin/p/10756548.html
.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總?http://www.csharpkit.com?
總結(jié)
以上是生活随笔為你收集整理的使用Entity Framework Core访问数据库(Oracle篇)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET Core 迁移躺坑记续集--W
- 下一篇: 使用Entity Framework C