- Published on
EntityFramework - Override SaveChange
public partial class ContosoUniversityEntities
{
public override int SaveChanges()
{
// 變更追蹤器,取得所有快取的物件
var entities = this.ChangeTracker.Entries();
foreach (var entity in entities)
{
if (entity.Entity is Course)
{
if (entity.State == EntityState.Modified)
{
//var ori = entity.OriginalValues.GetValue<string>("Title");
//var cur = entity.CurrentValues.GetValue<string>("Title");
//取得有修改欄位的原始值和修改後的值
entity.CurrentValues.SetValues(new { ModifiedOn = DateTime.Now });
}
}
}
return base.SaveChanges();
}
}