我有带有日期时间字段的模型。
我的代码正在使用模型设置此重置此字段到现在。但是我现在不想“设置”,而是现在“保存”:即如果我设置field1 = now [0:00:00]
,那么,10s。稍后field2 = now [0:00:10]
,10 秒。稍后,我执行SaveChanges()
我希望将两个字段都设置为 0:00:20(节省时间)。
编辑:代码示例:
var context = new SomeEntities();
SomeModel model = context.GetSomeModel(...);
// SomeModel has two DateTime fields: dt1 and dt2
model.dt1 = SavingDateTimeNow(); // .... 13:42:00
// some code, i.e. Sleep(TimeSpan.FromSeconds(10));
model.dt2 = SavingDateTimeNow(); // .... 13:42:10
// again 10s. code
context.SaveChanges(); // .... 13:42:20
Edit2:更复杂的例子:
var context = new SomeEntities();
SomeModel model = context.GetSomeModel(...);
// SomeModel has two DateTime fields: dt1 and dt2
if(someComplexCondition)model.dt1 = SavingDateTimeNow(); // .... 13:42:00
// some code from which I would know if someOtherComplexCondition is true
if(someOtherComplexCondition)model.dt2 = SavingDateTimeNow(); // .... 13:42:10
// again 10s. code
// much more code here
context.SaveChanges(); // .... 13:42:20
我希望(dt1 和 dt2)都设置为 ... 13:42:20,而不是 13:42:00 和 13:42:10
Edit3:最简单的例子:我希望它像 django 的字段一样工作auto_now=True
。