节约更新对象 [英] Update object on saving
本文介绍了节约更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个实体类(使用模型首先,如果该事项),应该坚持自己的最后一次更改日期。
I have an entity class (using model first, if that matters) that should persist its own last change date.
有没有一种方法来更新这个节约?现在,我只发现覆盖 OnPropertyChanged
但是这是不正确的替代,因为它是在加载对象时,也被称为。
Is there a way to update this on saving? Right now I have only found overriding OnPropertyChanged
but that is not the right override, as it is also called when loading the object.
推荐答案
您可以覆盖的SaveChanges您的ObjectContext派生类,只是在保存之前修改的最后更改日期。是这样的:
You can override SaveChanges on your ObjectContext derived class and modify the last change date just before saving. Something like:
public override int SaveChagnes(SaveOptions saveOptions)
{
var entities = ObjectStateManager.GetObjectStateEntries(EntityState.Modified)
.Where(e => !e.IsRelationship)
.Select(e => e.Entity)
.OfType<YourEntityType>();
var currentDate = DateTime.Now;
foreach(var entity in entities)
{
entity.LastChangedDate = currentDate;
}
return base.SaveChanges(saveOptions);
}
这篇关于节约更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文