实体框架/SQL2008 - 如何自动更新实体的 LastModified 字段? [英] Entity Framework/SQL2008 - How to Automatically Update LastModified fields for Entities?

查看:29
本文介绍了实体框架/SQL2008 - 如何自动更新实体的 LastModified 字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下实体:

public class PocoWithDates
{
   public string PocoName { get; set; }
   public DateTime CreatedOn { get; set; }
   public DateTime LastModified { get; set; }
}

哪个对应于具有相同名称/属性的 SQL Server 2008 表...

Which corresponds to a SQL Server 2008 table with the same name/attributes...

我如何自动:

  1. 将记录的 CreatedOn/LastModified 字段设置为now(在执行 INSERT 时)
  2. 将记录的 LastModified 字段设置为 now(执行 UPDATE 时)
  1. Set the CreatedOn/LastModified field for the record to now (when doing INSERT)
  2. Set the LastModified field for the record to now (when doing UPDATE)

当我说自动时,我的意思是我希望能够做到这一点:

When i say automatically, i mean i want to be able to do this:

poco.Name = "Changing the name";
repository.Save(); 

不是这个:

poco.Name = "Changing the name";
poco.LastModified = DateTime.Now;
repository.Save();

在幕后,某事"应该自动更新日期时间字段.那是什么东西"?

Behind the scenes, "something" should automatically update the datetime fields. What is that "something"?

我正在使用 Entity Framework 4.0 - EF 是否可以自动为我执行此操作?(也许是 EDMX 中的特殊设置?)

I'm using Entity Framework 4.0 - is there a way that EF can do that automatically for me? (a special setting in the EDMX maybe?)

从 SQL Server 端,我可以使用 DefaultValue,但这仅适用于 INSERT 的(不是 UPDATE 的).

From the SQL Server side, i can use DefaultValue, but that will only work for INSERT's (not UPDATE's).

同样,我可以使用 POCO 上的构造函数设置默认值,但这同样仅在实例化对象时有效.

Similarly, i can set a default value using a constructor on the POCO's, but again this will only work when instantiating the object.

当然我可以使用触发器,但它并不理想.

And of course i could use Triggers, but it's not ideal.

因为我使用的是实体框架,所以我可以连接到 SavingChanges 事件并在这里​​更新日期字段,但问题是我需要意识到"POCO(目前,我的存储库是用泛型实现的).我需要做一些面向对象的技巧(比如让我的 POCO 实现一个接口,然后调用一个方法).我并不反对,但如果我必须这样做,我宁愿手动设置字段.

Because i'm using Entity Framework, i can hook into the SavingChanges event and update the date fields here, but the problem is i need to become "aware" of the POCO's (at the moment, my repository is implemented with generics). I would need to do some sort of OO trickery (like make my POCO's implement an interface, and call a method on that). I'm not adversed to that, but if i have to do that, i would rather manually set the fields.

我主要是在寻找 SQL Server 2008 或 Entity Framework 4.0 解决方案.(或智能 .NET 方式)

I'm basically looking for a SQL Server 2008 or Entity Framework 4.0 solution. (or a smart .NET way)

有什么想法吗?

编辑

感谢@marc_s 的回答,但我选择了一个更适合我的场景的解决方案.

Thanks to @marc_s for his answer, but i went with a solution which is better for my scenario.

推荐答案

因为我有一个服务层在我的控制器(我使用 ASP.NET MVC)和我的存储库之间进行调解,所以我决定在这里自动设置字段.

As i have a service layer mediating between my controllers (im using ASP.NET MVC), and my repository, i have decided to auto-set the fields here.

此外,我的 POCO 没有关系/抽象,它们是完全独立的.我想保持这种方式,不标记任何虚拟属性,也不创建基类.

Also, my POCO's have no relationships/abstractions, they are completely independant. I would like to keep it this way, and not mark any virtual properties, or create base classes.

所以我创建了一个接口,IAutoGenerateDateFields:

So i created an interface, IAutoGenerateDateFields:

public interface IAutoGenerateDateFields
{
   DateTime LastModified { get;set; }
   DateTime CreatedOn { get;set; }
}

对于我希望自动生成这些字段的任何 POCO,我实现了这个接口.

For any POCO's i wish to auto-generate these fields, i implement this inteface.

使用我的问题中的示例:

Using the example in my question:

public class PocoWithDates : IAutoGenerateDateFields
{
   public string PocoName { get; set; }
   public DateTime CreatedOn { get; set; }
   public DateTime LastModified { get; set; }
}

在我的服务层,我现在检查具体对象是否实现了接口:

In my service layer, i now check if the concrete object implements the interface:

public void Add(SomePoco poco)
{
   var autoDateFieldsPoco = poco as IAutoGenerateDateFields; // returns null if it's not.

   if (autoDateFieldsPoco != null) // if it implements interface
   {
      autoDateFieldsPoco.LastModified = DateTime.Now;
      autoDateFieldsPoco.CreatedOn = DateTime.Now;
   }

   // ..go on about other persistence work.
}

稍后我可能会在 Add out to a helper/extension 方法中破坏该代码.

I will probably break that code in the Add out to a helper/extension method later on.

但我认为这对我的场景来说是一个不错的解决方案,因为我不想在保存时使用虚拟(因为我使用的是工作单元、存储库和纯 POCO),并且不想使用触发器.

But i think this is a decent solution for my scenario, as i dont want to use virtuals on the Save (as i'm using Unit of Work, Repository, and Pure POCO's), and don't want to use triggers.

如果您有任何想法/建议,请告诉我.

If you have any thoughts/suggestions, let me know.

这篇关于实体框架/SQL2008 - 如何自动更新实体的 LastModified 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆