如何在 EF Code First 中创建/更新 LastModified 字段

发布于 2024-11-07 13:37:27 字数 196 浏览 0 评论 0原文

我想添加一个列,用于在将记录保存到磁盘时存储当前的日期时间。实现这一目标的最佳方法是什么?

我知道这不是一个非常复杂的问题,但我想知道 EF 是否有任何最佳实践或任何功能可以简化任务。 例如:

  • 有没有办法将该字段的逻辑包含在表类中,以便在保存到磁盘时自动更新?
  • 修改或保存对象时是否需要捕获任何事件?

I want to add a column that stores the current DateTime when a record is saved to disk. What would be the best way to accomplish this?

I know it's not a very complex problem, but I wondered if there is any best practice or any feature of EF that would simplify the task.
For example:

  • Is there any way to include the logic for this field inside the table Class, so that it's automatically updated whenever it's saved to disk?
  • Is there any event to capture when an object is modified or saved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-11-14 13:37:27

一种选择是创建存储库层并实现您自己的 SaveChanges

public void SaveChanges()
{
    foreach (var entry in Context.ChangeTracker.Entries<ICreatedAt>().Where(x => x.State == EntityState.Added && x.Entity.CreatedAt == default(DateTime)))
            entry.Entity.CreatedAt = DateTime.Now;

    foreach (var entry in Context.ChangeTracker.Entries<ICreatedBy>().Where(x => x.State == EntityState.Added && x.Entity.CreatedBy == null))
        entry.Entity.CreatedBy = ContextManager.CurrentUser;

    foreach (var entry in Context.ChangeTracker.Entries<IModifiedAt>().Where(x => x.State == EntityState.Modified))
        entry.Entity.ModifiedAt = DateTime.Now;

    foreach (var entry in Context.ChangeTracker.Entries<IModifiedBy>().Where(x => x.State == EntityState.Modified))
        entry.Entity.ModifiedBy = ContextManager.CurrentUser;

    Context.SaveChanges();
}

one option would be to create repository layer and implement your own SaveChanges

public void SaveChanges()
{
    foreach (var entry in Context.ChangeTracker.Entries<ICreatedAt>().Where(x => x.State == EntityState.Added && x.Entity.CreatedAt == default(DateTime)))
            entry.Entity.CreatedAt = DateTime.Now;

    foreach (var entry in Context.ChangeTracker.Entries<ICreatedBy>().Where(x => x.State == EntityState.Added && x.Entity.CreatedBy == null))
        entry.Entity.CreatedBy = ContextManager.CurrentUser;

    foreach (var entry in Context.ChangeTracker.Entries<IModifiedAt>().Where(x => x.State == EntityState.Modified))
        entry.Entity.ModifiedAt = DateTime.Now;

    foreach (var entry in Context.ChangeTracker.Entries<IModifiedBy>().Where(x => x.State == EntityState.Modified))
        entry.Entity.ModifiedBy = ContextManager.CurrentUser;

    Context.SaveChanges();
}
故事还在继续 2024-11-14 13:37:27

不幸的是,没有您可以订阅的活动。我的建议是重写您的上下文中的 SaveChanges 方法,并在那里执行自定义逻辑(如果您想保持干净,您可以公开您自己的事件)

编辑

我为此选择的一个解决方案是让我的所有实体继承自一个公共对象基类(例如EntityBase)公开方法BeforeSave()和AfterSave(),您可以在重写SaveChanges方法时在自定义DbContext中调用(查看ChangeTracker以获取所有修改的条目)

Unfortunatly there are no events which you can subscribe to. My recommendation is to override the SaveChanges method in your context and do your custom logic there (if you want to keep it clean, you can expose your own events)

EDIT

One solution i picked for this was to let all my entities inherit from a common base class (e.g. EntityBase) which exposes the methods BeforeSave() and AfterSave() which you can call in your custom DbContext when overriding the SaveChanges method ( look at the ChangeTracker for all modified entries )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文