.NET Core 中使用 EF Core For PGSQL
開發環境:
系統:win10 家庭普通版
開發工具:VS2019 社區版
目標框架:DotNet Core 3.1
前言
本文介紹在.NET Core專案中使用EF Core(ORM框架 又叫 物件和關系的映射器)針對于PG(PostgreSQL)資料庫的增刪查改的操作,
1準備專案
在vs中創建一個ASP.NET Core的專案,并NuGet程式包管理器中安裝以下程式包
Npgsql.EntityFrameworkCore.PostgreSQL
Npgsql.EntityFrameworkCore.PostgreSQL.Design
Microsoft.EntityFrameworkCore.Tools
2 逆向工程,生成代碼

打開 程式包管理器控制臺(工具=>NuGet包管理器=>程式包管理器控制臺)
在PM> 后面 鍵入Scaffold-DbContext ‘Server=127.0.0.1;Port=5432;User Id=your User;
Password=your Password;Database=electricity;’ Npgsql.EntityFrameworkCore.PostgreSQL -ContextDir Data -OutputDir Models 并回車


這時候可以看到相應的目錄下已經幫我生成了類檔案,這些類檔案都是于上面圖片顯示的資料庫中的表是對應的,

3 進行增刪查改
增加記錄(Users表):
electricityContext EleContext = new electricityContext();
EleContext.Users.Add(new Users { Id = 3, Name = "xl", RealName = "liming" });//添加一條
//EleContext.Users.AddRange(new Users[2] { new Users { Id = 3, Name = "xl", RealName = "xiaoli" }, new Users { Id = 4, Name = "xl", RealName = "xiaoli" } });//添加一組記錄
EleContext.SaveChanges();//同步到資料庫
洗掉記錄(Users表):
/洗掉Users表中id==3的記錄*********/
electricityContext EleContext = new electricityContext();
var u1 = from user in EleContext.Users//LinQ陳述句
where user.Id == 3
select user;
if (u1.Count() > 0)
{
EleContext.Users.Remove(u1.FirstOrDefault());//刪掉與u1中的第一條相匹配的一條記錄
// EleContext.Users.RemoveRange(u1);//刪掉一組記錄
}EleContext.SaveChanges();//同步到資料庫
查詢記錄(Users表):
electricityContext EleContext = new electricityContext();
var u2=EleContext.Users.Where(d=>d.Name=="xl");//查詢姓名為xl的記錄
更改記錄(Users表):
/更改Users表中id==3的第一條記錄的名字為lm********/
electricityContext EleContext = new electricityContext();
Users u = EleContext.Users.Where(Users => Users.Id == 3).FirstOrDefault();
u.Name = "lm";
EleContext.Set<Users>().Update(u);
EleContext.SaveChanges();//同步到資料庫
4 踩坑記錄
1、記錄更改問題
錯誤的:
electricityContext EleContext = new electricityContext();
EleContext.Users.Add(new Users { Id = 3, Name = "xl", RealName = "liming" });//添加一條
EleContext.SaveChanges();//同步到資料庫
Users u=new Users {Id = 3, Name = "lm", RealName = "liming" };
EleContext.Set<Users>().Update(u);
EleContext.SaveChanges();//同步到資料庫
正確的方法1:
electricityContext EleContext = new electricityContext();
EleContext.Users.Add(new Users { Id = 3, Name = "xl", RealName = "liming" });//添加一條
EleContext.SaveChanges();//同步到資料庫
Users u = EleContext.Users.Where(Users => Users.Id == 3).FirstOrDefault();
u.Name = "lm";
EleContext.Set<Users>().Update(u);
EleContext.SaveChanges();//同步到資料庫
正確的方法2:
electricityContext EleContext = new electricityContext();
EleContext.Users.Add(new Users { Id = 3, Name = "xl", RealName = "liming" });//添加一條
EleContext.SaveChanges();//同步到資料庫
EleContext = new electricityContext();
Users u=new Users {Id = 3, Name = "lm", RealName = "liming" };
EleContext.Set<Users>().Update(u);
EleContext.SaveChanges();//同步到資料庫
2、生成代碼時的失敗

解決方法:
在生成代碼前,跑一下專案,看看是否有報錯誤,把有錯誤的地方修正后,再次生成即可成功,
未完,待續,,,
結語
最近在學EF Core+PG資料庫,碰到了一些問題(其中不知道怎么生成代碼是我覺得最大的坑),所將自己碰到的坑以及解決方法分享給大家,希望對大家能有所幫助,
文章中若有錯誤的地方請指正,我會非常感謝,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/153018.html
標籤:其他
下一篇:oracle資料庫語法總結
