实体框架代码第一个DateTime字段更新修改 [英] Entity Framework Code First DateTime field update on modification

查看:129
本文介绍了实体框架代码第一个DateTime字段更新修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体:

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string UserAddress { get; set; }
    public DateTime CreateDate { get; set; }
    public DateTime? UpdateDate { get; set; }
}

如何使 UpdateDate 用户实体的c $ c>字段成为更新时的服务器 DateTime 也就是说,每当数据库中的实体被修改时, UpdateDate 字段将更改为确切的日期和时间,以及如果说 UpdateDate = new DateTime(Datetime.Now.Ticks)

How can I make the UpdateDate field of the User entity become the server DateTime on an update? That is to say, whenever an entity in the database becomes modified, the UpdateDate field changes to that exact date and time as well as if to say UpdateDate = new DateTime(Datetime.Now.Ticks)

推荐答案

首先,考虑 DateTimeOffset 是一个更好的选择,如果你要存储本地时间。或者,您可以使用基于UTC的 DateTime ,但在此示例中,我将显示 DateTimeOffset 。为此不要存储本地的 DateTime ,因为您将在时区和夏令时出现问题。另请参见:反对DateTime.Now的案例

First, consider that DateTimeOffset is a better choice if you are going to be storing local times. Alternatively, you could use a UTC-based DateTime, but I will show DateTimeOffset in this example. Never store a local-based DateTime for this purpose, as you will have issues with time zones and daylight saving time. See also: The Case Against DateTime.Now.

接下来,考虑一个通用的界面,可以用于您希望跟踪创建/更新值的实体。

Next, consider a common interface that you can use for entities that you wish to track created/updated values.

public interface IDatedEntity
{
    DateTimeOffset Created { get; set; }
    DateTimeOffset Updated { get; set; }
}

将该界面及其属性应用于您正在使用的实体。 / p>

Apply that interface and its properties to the entities you are working with.

public class User : IDatedEntity
{
    // ...

    public DateTimeOffset Created { get; set; }
    public DateTimeOffset Updated { get; set; }
}

现在在数据库环境中,您可以附加到 SavingChanges 事件并应用所需的行为:

Now in your database context, you can attach to the SavingChanges event and apply the desired behavior:

public class MyContext : DbContext
{
    public DbSet<User> Users { get; set; }

    public MyContext()
    {
        var objectContext = ((IObjectContextAdapter)this).ObjectContext;
        objectContext.SavingChanges += (sender, args) =>
        {
            var now = DateTimeOffset.Now;
            foreach (var entry in this.ChangeTracker.Entries<IDatedEntity>())
            {
                var entity = entry.Entity;
                switch (entry.State)
                {
                    case EntityState.Added:
                        entity.Created = now;
                        entity.Updated = now;
                        break;
                    case EntityState.Modified:
                        entity.Updated = now;
                        break;
                }
            }
            this.ChangeTracker.DetectChanges();
        };
    }
}

请注意,最终调用 DetectChanges ,这将确保新的更新日期值适用于所有修改的实体。

Note the final call to DetectChanges, which will ensure that the new updated date values are applied to all modified entities.

这篇关于实体框架代码第一个DateTime字段更新修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆