db.CodeFirst.InitTables<UserInfo001>(); 失败 返回

SqlSugar 待处理
2 482
该叫什么 Hayes 发布于2026/1/23
悬赏:5 飞吻
void Main()
{
    //注册DLL防止找不到DLL
    InstanceFactory.CustomAssemblies = new System.Reflection.Assembly[] {
            typeof(SqlSugar.DuckDB.DuckDBProvider).Assembly };

    //创建DB
    var db = new SqlSugarClient(new ConnectionConfig()
    {
        IsAutoCloseConnection = true,
        DbType = SqlSugar.DbType.DuckDB,
        ConnectionString = "DataSource = train_services.db",
        LanguageType = LanguageType.Default//Set language

    },
    it =>
    {
        // Logging SQL statements and parameters before execution
        // 在执行前记录 SQL 语句和参数
        it.Aop.OnLogExecuting = (sql, para) =>
        {
            Console.WriteLine(UtilMethods.GetNativeSql(sql, para));
        };
    });

    db.CodeFirst.InitTables<UserInfo001>();

    var dataList = new List<UserInfo001>();

    for (int i = 0; i < 100; i++)
    {
        dataList.Add(new UserInfo001 { UserName = "zhan" + i, Context = "", Email = "", Price = 8 + i, RegistrationDate = DateTime.Now });
    }


    // 5. 写入数据(批量插入)
    int insertCount = db.Insertable(dataList).ExecuteCommand();
    Console.WriteLine($"成功写入 {insertCount} 条数据到数据库!");
}

 
public class UserInfo001
{
    /// <summary>
    /// User ID (Primary Key)
    /// 用户ID(主键)
    /// </summary>
    [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
    public int UserId { get; set; }

    /// <summary>
    /// User name
    /// 用户名
    /// </summary>
    [SugarColumn(Length = 50, IsNullable = false)]
    public string UserName { get; set; }

    /// <summary>
    /// User email
    /// 用户邮箱
    /// </summary>
    [SugarColumn(IsNullable = true)]
    public string Email { get; set; }


    /// <summary>
    /// Product price
    /// 产品价格
    /// </summary> 
    public decimal Price { get; set; }

    /// <summary>
    /// User context
    /// 用户内容
    /// </summary>
    [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString, IsNullable = true)]
    public string Context { get; set; }

    /// <summary>
    /// User registration date
    /// 用户注册日期
    /// </summary>
    [SugarColumn(IsNullable = true)]
    public DateTime? RegistrationDate { get; set; }
}

报错 


[Sql]:select cast(relname as varchar) as Name,
                        cast(obj_description(c.oid,'pg_class') as varchar) as Description from pg_class c
                         inner join
                         pg_namespace n on n.oid = c.relnamespace and nspname='main'
                         inner join
                         pg_tables z on z.tablename=c.relname
                        where  relkind in('p', 'r') and relname not like 'hg_%' and relname not like 'sql_%' and schemaname='main' order by relname


[Sql]: 
                    CREATE SEQUENCE IF NOT  EXISTS "mainUserInfo001_UserId_sequence"


[Sql]:CREATE TABLE "userinfo001"(
"userid" INTEGER NOT NULL   DEFAULT NEXTVAL('mainUserInfo001_UserId_sequence') ,
"username" TIMESTAMP(50) NOT NULL  ,
"email" TIMESTAMP(255) DEFAULT NULL  ,
"price" DOUBLE NOT NULL  ,
"context" text DEFAULT NULL  ,
"registrationdate" DECIMAL DEFAULT NULL   , Primary key("userid"))

SqlSugarException•••
Parser Error: TIMESTAMP only supports until nano-second precision (9)
MessageParser Error: TIMESTAMP only supports until nano-second precision (9)
InnerExceptionnull
InnerExceptionnull
StackTracenull
StackTrace

   at SqlSugar.Check.Exception(Boolean isException, String message, String[] args)

   at SqlSugar.CodeFirstProvider.InitTables(Type entityType)

   at SqlSugar.CodeFirstProvider.InitTables[T]()

   at 

UserQuery.Main(), line 26

Data(0 items)
HelpLinknull
HResult-2146233088
Parametresnull
Sourcenull
SourceSqlSugar
Sqlnull
TargetSitenull
TargetSite

System.Reflection.MethodBase


热忱回答2

  • Hayes Hayes VIP0
    2026/1/23

    image.png经过测试 5.1.4.198的可以正常生成数据,但是 DuckDBCore  5.1.4.205 和 SqulsugarCore 5.1.4.211 这个代码就会提示这个错误

    0 回复
  • Hayes Hayes VIP0
    2026/1/26

    image.png

    0 回复