BulkCopy 时 Can't change NpgsqlDbType from Unknown to Double” 返回

SqlSugar 处理完成
11 329
using SqlSugar;

namespace SqlSugarClient_Demo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            SqlSugarClient BDb = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.PostgreSQL,
                ConnectionString = string.Format(@""),
                IsAutoCloseConnection = true
            },
            db =>{
                db.Aop.OnLogExcuting = (sql, pars) =>{};
            });
            
             var lstStudents = new List<Student>();
          
             lstStudents.Add(new Student() { Id =1,Height= null,Name="张三"});
             lstStudents.Add(new Student() { Id = 1, Height = 2.3, Name = "李四" });
             BDb.Fastest<Student>().BulkCopy(lstStudents);
        }
    }
     public class Student
    {
       [SugarColumn(IsPrimaryKey = true)]
        
         public int Id { get; set; }
       
         public double? Height { get; set; }
         
         public string Name { get; set; }
     }
}

demo如上,当数据库可空的字段 类型float8,在定义实体类时

public double? Height

当想插入的记录集合 该字段既有null 又有数值时,使用BulkCopy时会抛异常 Can't change NpgsqlDbType from Unknown to Double”

热忱回答11

  • 数据库为PG

    0 回复
  • 建表语句加上

    0 回复
  • 数据库要加可空

    0 回复
  • 目前不能重现

    0 回复
  • image.png

    0 回复
  •     using SqlSugar;
        using System.Collections.Generic;
    
        namespace SqlSugarClient_Demo
        {
            public class Program
            {
                public static void Main(string[] args)
                {
                    SqlSugarClient BDb = new SqlSugarClient(new ConnectionConfig()
                    {
                        DbType = SqlSugar.DbType.PostgreSQL,
                        ConnectionString = Config.ConnectionString,
                        IsAutoCloseConnection = true
                    },
                    db => {
                        db.Aop.OnLogExecuting = (sql, pars) => { };
                    });
    
                    var lstStudents = new List<Student111>();
                    BDb.CodeFirst.InitTables<Student111>();
                    BDb.DbMaintenance.TruncateTable<Student111>();
                    lstStudents.Add(new Student111() { Id = 12, Height = null, Name = "张三" });
                    lstStudents.Add(new Student111() { Id = 13, Height = 2.31, Name = "李四" });
                    BDb.Fastest<Student111>().BulkCopy(lstStudents);
                }
            }
            [SugarTable("aaa")]
            public class Student111
            {
                [SugarColumn(IsPrimaryKey = true)]
    
                public int Id { get; set; }
                [SqlSugar.SugarColumn(ColumnDataType = "float8",IsNullable =true)]
                public double? Height { get; set; }
    
                public string Name { get; set; }
            }
        }


    这个是完整测试还有疑问在我用例修改让他能重现


    0 回复
  • 直接跑就行了,直接建表

    0 回复
  • sqlsugar用最新的

    0 回复
  • 可以重现了 ,一会修复

    0 回复
  • CREATE TABLE byd_dnt_sd.student (

    id int8 NOT NUll,

    height float8 NULL,

    "name" varchar NULL,

    );

    0 回复
  • SqlSugarCore 5.1.4.161-preview08


    已修复 ,过五分钟后安装

    0 回复