ASP.NET Core 中使用 EF Core For PGSQL

.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数据库,碰到了一些问题(其中不知道怎么生成代码是我觉得最大的坑),所将自己碰到的坑以及解决方法分享给大家,希望对大家能有所帮助。
文章中若有错误的地方请指正,我会非常感谢。

猜你喜欢

转载自blog.csdn.net/m0_46202128/article/details/108895273