IsOnlyIgnoreInsert在BulkCopy中失效,导致插入了C#类型的默认值 返回

SqlSugar 沟通中
2 224

// 数据库 SQL Server 2025,Sql Sugar版本 5.1.4.215


// 模型字段

[Tenant(DbConnectionStringNames.Log)]
[SugarTable("LoginLog")]
public sealed class LoginLogEntity : IHasCreatedAt
{
    // 省略无关字段...
   
    /// <summary>
    /// 日志写入的时间。
    /// </summary>
    [SugarColumn(DefaultValue = "GETDATE()", IsOnlyIgnoreInsert = true, IsOnlyIgnoreUpdate = true)]
    public DateTime CreatedAt { get; set; }
}


// 使用方

public override async Task WriteLogAsync(LoginLogEntity[] logs)
{
    using var dbClient = CreateTenantDbClient().GetConnectionWithAttr<LoginLogEntity>();
    var list = new List<LoginLogEntity>(logs);
    _ = await dbClient.Fastest<LoginLogEntity>().BulkCopyAsync(list);
}


// 生成的SQL

insert bulk [LoginLog] (/* 省略无关字段 */, [CreatedAt] DateTime)


// 实际生成的SQL包含了CreatedAt字段,导致写入了C#中的默认值1970-01-01


热忱回答2

  • Liang Liang VIP0
    2周前
     [SugarColumn(InsertServerTime = true)]
        public DateTime CreatedAt { get; set; }



    0 回复
  • BulkCopy不支持。

    0 回复