FreeSql
FreeSql copied to clipboard
insert 语句生成的sql 手动赋值的Datetime类型字段 被默认值覆盖
使用的freesql版本: 2.5.200
drop table if exists aaa
;
create table aaa
(
Id
bigint(20) not null auto_increment comment '自增ID',
CreateTime
datetime not null default current_timestamp comment '记录时间',
primary key (Id
)
) engine = InnoDB auto_increment = 1 default charset=utf8 comment = '记录表';
生成的实体类时 public class aaa{ [JsonProperty, Column(IsPrimary = true, IsIdentity = true)] public string Id { get; set;} [JsonProperty, Column(DbType = "datetime" , InsertValueSql = "CURRENT_TIMESTAMP")] public string Id { get; set;} }
插入语句 var a = new aaa{CreateTime=datetime.today} XXXDBContext.Insert(a).NoneParameter().ExecuteAffrowsAsync();
//生成的sql语句 insert into a ('CreateTime') values(CURRENT_TIMESTAMP)
期望的效果: 我手动赋值了CreateTime字段, 生成的sql就不要用默认值了
InsertValueSql 优先级更高。
很不科学的一个设计... 数据库的字典只是默认值 不填写的时候才会使用,InsertValueSql 却是一个强制值
有空调试下代码,可以了解更多,有些方法不适合写到文档。
机制不能随便改,不然前面看过代码的人等于白看了。
XXXDBContext.Insert(a)
.NoneParameter()
.IgnoreColumns(new string[0])
.ExecuteAffrowsAsync();
@yinyihao1 @bbenph
InsertValueSql 也只是默认被忽略了而已,可以调用 .IgnoreColumns(new string[0]) 去掉默认设置。
首先感谢回复 ! .IgnoreColumns 我的理解是不插入这个字段了吧 例如 table a,b,c IgnoreColumns(b) 生成的sql 就是 insert into table (a,c) values(……)
并没有达到想要的效果。
@2881099
@yinyihao1 @bbenph
InsertValueSql 也只是默认被忽略了而已,可以调用 .IgnoreColumns(new string[0]) 去掉默认设置。
我使用的dbfirst 模式,例如sql server 中的默认值
DEFAULT ('') FOR [Name]
生成的属性是
public string Name { get; set; } = "";
DEFAULT (getdate()) FOR [cActionTime]
对应生成的属性就变成
[Column(Name = "ActionTime",InsertValueSql ="getdate()")] public DateTime ActionTime { get; set; }
这里是不是存在逻辑行为不一致的情况。 我觉得 同字符类型一样的处理
public DateTime ActionTime { get; set; } = DateTime.Now
是不是更好
@yinyihao1 @bbenph 看错了,忽略我上面的回复
字符串可以转换成 ""
"getdate()" 逆向解析成 DateTime.Now 会比较困难,所以才会生成那样。