.NET ORM操作 PostgreSQL PGSQL

字符串格式  PORT=5432;DATABASE=SqlSugar4xTest;HOST=localhost;PASSWORD=haosql;USER ID=postgres


1、不规范表和规范表

2种用法小有区别

1.1 规范(小写表) 

推荐: 写Sql无需考虑大小定

 //无需配置任何东西
 SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
 {
       DbType = DbType.SqlServer,
       ConnectionString = Config.ConnectionString3,
       IsAutoCloseConnection = true
 });
 
 
//技巧:如果想全自动 StudentName变成 student_name
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
  {
       DbType = DbType.SqlServer,
       ConnectionString = Config.ConnectionString3,
       IsAutoCloseConnection = true,
       ConfigureExternalServices=new ConfigureExternalServices() 
       {
           EntityService = (x,p) => //处理列名
           {
             //最好排除DTO类
             p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName);//ToUnderLine驼峰转下划线方法
           },
           EntityNameService = (x, p) => //处理表名
           {
              //最好排除DTO类
              p.DbTableName=UtilMethods.ToUnderLine(p.DbTableName);//ToUnderLine驼峰转下划线方法
           }
        }
   });

 2.2 不规范表(不自动转小写

类是什么样,表就什么样,大小写和数据库一模一样

缺点:写SQL难受 ,需要大小写和实体一样,并且加上转译符号

var db= new SqlSugarClient(new ConnectionConfig(){
           DbType = SqlSugar.DbType.PostgreSQL,
           ConnectionString = Config.ConnectionString,
           IsAutoCloseConnection = true,
           MoreSettings= new ConnMoreSettings()
           {
             PgSqlIsAutoToLower = false,//增删查改支持驼峰表
             PgSqlIsAutoToLowerCodeFirst = false, // 建表建驼峰表。5.1.3.30 
           }})
//注意 是false不要看错了


2、JSON类型

PgSql 的Json类型操作实体需要配置

//可以是JSON对应的对象  
[SugarColumn(IsJson = true )]//低版本要加 ColumnDataType ="json"
Public List<Order> JsonText{get;set;} //只要能序列化成json的所有对象都可以

Json函数:https://www.donet5.com/Home/Doc?typeId=1232


3、数组类型

 [SugarColumn(ColumnDataType = "text []", IsArray = true)]  
public string [] MenuIds { get; set; }


4、geometry / postgis

Nuget安装 Npgsql.NetTopologySuite7.0以下版和SqlSugarCore

//实体
[SugarColumn(ColumnName="geom")]
public Geometry geom { get; set; }

//代码    
          //使用长链接 (如果不使用OpenAways需要将自动释放关闭手动Open Close)
          using (db.Ado.OpenAlways()) {
   

             (db.Ado.Connection as  NpgsqlConnection).TypeMapper.UseNetTopologySuite(
              new DotSpatialAffineCoordinateSequenceFactory(Ordinates.XY),
              handleOrdinates: Ordinates.XY); //配置相关代码


             var list=db.Queryable<xx>().ToList();//查询插入就可以用了
            
             
            }
//注意:Npgsql.NetTopologySuite7.0以下版本,不要高版本,高版本写法有变你们可以研究。


5、时间异常

Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', 

only UTC is supported. Note that it's not possible to mix DateTimes with different 

Kinds in an array/range. See the Npgsql.EnableLegacyTimestampBehavior

 AppContext switch to enable legacy behavior.

设置下面几个看看

AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);

https://github.com/npgsql/doc/blob/main/conceptual/Npgsql/types/datetime.md/


6、架构的支持 5.0.9.1版本支持

连接字符串上加上 searchpath=架构名  ,可以支持多架构  (5.0.9.1版本支持


7、自增配置

如果不使用CodeFirst, 建表实现自增应该下面这样写

DROP TABLE IF EXISTS "public"."order";
CREATE TABLE "public"."order" (
  "id" int4 NOT NULL DEFAULT nextval('order_id_seq'::regclass),
  "name" varchar(200)   NOT NULL,
  "price" numeric NOT NULL,
  "createtime" timestamp(6),
  "customid" int4
)


文档:SqlSugar5.0