db.CodeFirst.InitTables<UserInfo001>(); 失败 返回
Hayes 发布于2026/1/23
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) | |
| Message | Parser Error: TIMESTAMP only supports until nano-second precision (9) |
|---|---|
| InnerException | null |
| InnerException | null |
| StackTrace | null |
| StackTrace | at SqlSugar.Check.Exception(Boolean isException, String message, String[] args) at SqlSugar.CodeFirstProvider.InitTables(Type entityType) at SqlSugar.CodeFirstProvider.InitTables[T]() at |
| Data | (0 items) |
| HelpLink | null |
| HResult | -2146233088 |
| Parametres | null |
| Source | null |
| Source | SqlSugar |
| Sql | null |
| TargetSite | null |
| TargetSite | |
热忱回答(2)
-
Hayes VIP0
2026/1/23
经过测试 5.1.4.198的可以正常生成数据,但是 DuckDBCore 5.1.4.205 和 SqulsugarCore 5.1.4.211 这个代码就会提示这个错误0 回复 -
Hayes VIP0
2026/1/26
0 回复