DotNetNext / SqlSugar

.Net aot ORM Fastest ORM Simple Easy VB.NET Sqlite orm Oracle ORM Mysql Orm 虚谷数据库 postgresql ORm SqlServer oRm 达梦 ORM 人大金仓 ORM 神通ORM C# ORM , C# ORM .NET ORM NET5 ORM .NET6 ORM ClickHouse orm QuestDb ,TDengine ORM,OceanBase orm,GaussDB orm ,Tidb orm Object/Relational Mapping
https://www.donet5.com/Home/Doc
MIT License
5.27k stars 1.33k forks source link

提问或建议 针对DateTime的默认值 #1114

Closed Sheldon-NULL closed 1 year ago

Sheldon-NULL commented 1 year ago

当前是否已经实现对像CreateTime或UpdateTime这种字段,插入或更新能赋予当前时间的值呢?

我的想法如果能做到这种简单写完就能实现就很快

[SugarClumn(IsOnlyIgnoreInsert =true,DefaultVaule   = DateTime.Now.ToString())]
public DateTime? CreateTime { get; set; } 

希望能得到解决方案,或者能添加这个功能。

DotNetNext commented 1 year ago
  db.Aop.DataExecuting = (oldValue, entityInfo) =>
   {
      /*** inset生效 ***/
      if (entityInfo.PropertyName == "CreateTime"&&entityInfo.OperationType== DataFilterType.InsertByObject)
      {
         entityInfo.SetValue(DateTime.Now);//修改CreateTime字段
         //entityInfo有字段所有参数

         // get value 可以这么写
         // var value=entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
      }
      //特性用法
      //if(p.EntityColumnInfo.PropertyInfo.GetCustomAttribute<自定义特性>()!=null
      // &&entityInfo.OperationType== DataFilterType.InsertByObject)
      //5.1.3.23 更好的支持了特性
      //p.IsAnyAttribute<特性>()
      //p.GetAttribute<特性>()

      /*** update生效 ***/       
      if (entityInfo.PropertyName =="UpdateTime" && entityInfo.OperationType == DataFilterType.UpdateByObject)
      {
         entityInfo.SetValue(DateTime.Now);//修改UpdateTime字段
      }

      //根据当前列修改另一列 可以么写
      //if(当前列逻辑==XXX)
      //var properyDate = entityInfo.EntityValue.GetType().GetProperty("Date");
       //if(properyDate!=null)
      //properyDate.SetValue(entityInfo.EntityValue,1);
   };
DotNetNext commented 1 year ago

你可先用这个AOP方式实现

DotNetNext commented 1 year ago

image 这个功能目前只支持了插入,我这2天把更新也支持一下

Sheldon-NULL commented 1 year ago

非常感谢大佬

Sheldon-NULL commented 1 year ago

留着给您更新完,再关闭吧

DotNetNext commented 1 year ago

image

DotNetNext commented 1 year ago

最新预览本已支持了,更新可以试用了

Sheldon-NULL commented 1 year ago

SqlSugarCoreNoDrive这个包应该没上传更新吧

DotNetNext commented 1 year ago

没有,我现在上传

DotNetNext commented 1 year ago

已发布 勾一下预览

Sheldon-NULL commented 1 year ago

ok,已经用起来了。 然后一个小建议:我觉得这个属性还有优化空间,可以再考虑把DateTime.ToString(),这个有关控制时间格式的重载做进去,甚至像DateTime.Now.AddDays(),这种时间运算也不是不可以考虑的。

DotNetNext commented 1 year ago

这个也计划中,属性自定义方式

DotNetNext commented 1 year ago

查询 插入 更新 有套自定义体系,后面还要支持值对象

DotNetNext commented 1 year ago

这个先关闭有新问题在发新ISSUE