ASP.NET MVC 5 中的 DateCreated 和 DateModified [英] DateCreated and DateModified in ASP.NET MVC 5

查看:22
本文介绍了ASP.NET MVC 5 中的 DateCreated 和 DateModified的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事代码优先项目,我需要数据库来处理 DateCreatedDateModified.

I am working on a Code-First project, and I need database to handle DateCreated and DateModified.

该应用程序在开发机器上使用 LocalDB 的 IIS Express 上运行,并将在具有 IIS 7.5 的部署服务器上使用 SQL Server 2012.

The application is running on IIS Express with LocalDB on dev machine, and will be using SQL Server 2012 on deployment server with IIS 7.5.

我有以下模型:

public class Person : IdentityUser {

  [Required]
  public string Name { get; set; }

  public Date DateOfBirth { get; set; }

  public string Address { get; set; }

  [DatabaseGeneratedOption.Identity]
  public Date DateCreated { get; set; }

  [DatabaseGeneratedOption.Identity]
  public Date DateModified { get; set; }

}

请列出配置 DB 以处理事务元数据的确切步骤,例如需要在模型中设置的内容以及在 DB 上下文配置器中是否需要执行任何操作.我正在寻找类似的内容:关于 ASP.NET MVC 日期处理你需要知道的一切",但没有太多涉及这方面.

Please lay out exact steps to configure DB to handle the transaction meta dates, like what needs to be set in model and if there are any actions to be taken in DB context configurator. I was searching for something like: "All you need to know about ASP.NET MVC date handling", but couldn't much touching that aspect.

提前致谢.

推荐答案

我会从实体框架的角度考虑这个问题.

I will think about this from the Entity-framework Point of view.

您基本上需要执行以下操作:

You basically need to do the following :

1- 我将定义一个类似 ITrackable 接口的接口,如果这些模型需要跟踪 DateCreated 和 DateModified 属性,我将让我的模型实现该接口.

1- I will define an interface like ITrackable interface, and make my models implement that interface if these models need track the DateCreated and DateModified porperties.

2- 不知何故,你的模型知道它是否是添加/修改的实体,因为这将决定设置哪个属性(添加实体和修改实体的日期修改).

2- Somehow your model know whether it is am Added/Modified entities because this will decide which property to set (Both for Added entity and just DateModified for Modified entities).

3- 使用实体框架中的 DbContext 添加一个扩展方法,当您尝试通过 SaveChanges 或 SaveChangesAsync 保存实体时需要调用该方法,此方法将遍历跟踪的实体并根据以下内容设置 DateCreated 和 DateModified 属性实体的状态.

3- With your DbContext in Entity-Framework Add an extension method that you need to call when you try to save your entities through SaveChanges or SaveChangesAsync and this method will loop through the tracked entities and set the DateCreated and DateModified properties according to the entity's state.

所以您的模型将如下所示:

So your model will look like this:

public class Person : IdentityUser, ITrackable
{

    [Required]
    public string Name { get; set; }

    public Date DateOfBirth { get; set; }

    public string Address { get; set; }

    public DateTime DateCreated { get; set; }

    public DateTime DateModified { get; set; }
}

ITrackable 的样子

Where ITrackable looks like

public interface ITrackable
{
    public DateTime DateCreated { get; set; }

    public DateTime DateModified { get; set; }
}

并且扩展方法将是这样的:

and the extension method will be something like this:

internal static class ContextHelper
{
    internal static void SyncObjectsStatePreCommit(this DbContext dbContext)
    {
        foreach (var dbEntityEntry in dbContext.ChangeTracker.Entries())
        {

            // do any other stuff you want.
            // ..
            // ..

            // work with ITrackable entities
            var trackableObject = dbEntityEntry.Entity as ITrackable;

            // we need to set/update trackable properties
            if (trackableObject == null)
            {
                continue;
            }

            var dateTime = DateTime.Now;

            // set createddate only for added entities
            if (entityState.ObjectState == ObjectState.Added)
            {
                trackableObject.CreatedDate = dateTime;
            }

            // set LastUpdatedDate for any case other than Unchanged
            if (entityState.ObjectState != ObjectState.Unchanged)
            {
                trackableObject.ModifiedDate = dateTime;
            }
        }
    }
}

现在在您的 dbContext Save 方法中,例如您需要调用此方法来设置所有这些属性.

Now in your dbContext Save method for example you need to call this method to setup all these properties.

public override Task<int> SaveChangesAsync()
{
    this.SyncObjectsStatePreCommit();
    return base.SaveChangesAsync();
}

希望有所帮助.

这篇关于ASP.NET MVC 5 中的 DateCreated 和 DateModified的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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