首页 > 解决方案 > 在某个确切时间采取行动的应用程序

问题描述

假设我有一个模型

public class SomeModel
{
    public DateTime StartDate { get; set; }
    public bool IsActive { get; set; }
}

我的问题是:当 StartDate 中确定的时间到来时,将属性 IsActive 更改为 true 的最佳做法是什么?我应该使用委托/事件还是计时器?我将不胜感激一些简短的例子。

关键是 IsActive 存储在数据库中,因此在更改时我希望模型在 db 中更新。

标签: c#entity-frameworkasp.net-core

解决方案


不需要计时器,因为您不需要设置任何内容。只需编写一个计算IsActive.

public class SomeModel
{
    public DateTime StartDate { get; set; }
    [NotMapped]
    public bool IsActive { get; } => DateTime.UtcNow >= StartDate;

}

推荐阅读