ASP.NET MVC 5中的DateCreated和DateModified

我正在开发Code-First项目,我需要数据库来处理DateCreatedDateModified

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

我有以下型号:

 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日期处理” ,但是无法触及那个方面。

提前致谢。

我将从entity framework的角度考虑这个问题。

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

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

2-某种程度上,你的模型知道它是否是添加/修改实体,因为这将决定要设置的属性(添加实体和修改实体只有DateModified)。

3-在entity framework中使用DbContext添加在尝试通过SaveChanges或SaveChangesAsync保存实体时需要调用的扩展方法,此方法将循环遍历跟踪的实体并根据实体的状态设置DateCreated和DateModified属性。

所以你的模型看起来像这样:

 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 Save方法中,您需要调用此方法来设置所有这些属性。

希望有所帮助。

我发现上面的答案有点令人困惑,但我认为更直接的解决方案是覆盖此博客中描述的SaveChanges方法

此解决方案还使用ITrackable接口,当触发SaveChanges时,它会检查Entity是否实现此接口:

 public override System.Threading.Tasks.Task SaveChangesAsync() { foreach (var auditableEntity in ChangeTracker.Entries()) { if (auditableEntity.State == EntityState.Added || auditableEntity.State == EntityState.Modified) { // implementation may change based on the useage scenario, this // sample is for forma authentication. string currentUser = HttpContext.Current.User.Identity.GetUserId(); DateTime currentDate = SiteHelper.GetCurrentDate(); // modify updated date and updated by column for // adds of updates. auditableEntity.Entity.ModifiedDateTime = currentDate; auditableEntity.Entity.ModifiedUserId = currentUser; // pupulate created date and created by columns for // newly added record. if (auditableEntity.State == EntityState.Added) { auditableEntity.Entity.CreatedDateTime = currentDate; auditableEntity.Entity.CreatedUserId = currentUser; } else { // we also want to make sure that code is not inadvertly // modifying created date and created by columns auditableEntity.Property(p => p.CreatedDateTime).IsModified = false; auditableEntity.Property(p => p.CreatedUserId).IsModified = false; } } } return base.SaveChangesAsync(); }