【发布时间】:2014-02-20 15:21:46
【问题描述】:
如果我有以下实体:
public class PocoWithDates {
public string PocoName { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
}
对应同名/属性的 SQL Server 2008 (R2) 表...
我怎样才能自动:
- 在执行 INSERT 时将 DateCreated 和 DateUpdated 字段设置为现在
- 在进行更新时将DateUpdated 字段设置为现在
当我自动说的时候,我的意思是我希望能够做到这一点:
poco.Name = "Changing the name";
repository.Save();
不是这个:
poco.Name = "Changing the name";
poco.LastModified = DateTime.Now;
repository.Save();
在幕后,“某事”应该会自动更新 DateTime 字段。那“东西”是什么?我正在使用实体框架 6.0。 EF有没有办法自动做到这一点?我知道 MySQL 可以做这样的事情。或者在 SQL Server 中使用触发器/存储过程。但我不想那样。
这个问题是这个问题的替代和修改版本: Entity Framework/SQL2008 - How to Automatically Update LastModified fields for Entities?
这个问题为 EF 4.0 提供了一些很好的解决方案/答案。但我希望它用于带有DBContext 的 EF6.0。如果我有更多时间,我会尝试调整这些解决方案并让它们在 EF6.0 中运行。但现在我想知道是否有人已经解决了这个问题,或者知道如何将 EF4.0 的自定义 SaveChanges() 或 Add() 覆盖转录为 EF6.0。
【问题讨论】:
标签: c# .net sql-server entity-framework entity-framework-6