dateCreated会和DateModified在ASP.NET MVC 5 [英] DateCreated and DateModified in ASP.NET MVC 5

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

问题描述

我工作的一个code-第一个项目,我需要数据库来处理 dateCreated会 DateModified

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

该应用程序与IIS的LocalDB防爆preSS开发计算机上运行,​​并且将使用SQL Server 2012部署服务器上与IIS 7.5。

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 - 不知怎的,你的模型知道是否是我添加/修改实体,因为这将决定设置哪个属性(无论是对新增实体,只是为DateModified修改实体)。

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; }
}

和扩展方法是这样的:

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保存方法,例如,你需要调用这个方法来设置所有这些属性。

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

希望有所帮助。

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

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