问题描述
如果我有以下实体:
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...
我怎样才能自动
- 将CreatedOn /上次更改时间字段中记录为现在(做INSERT时)
- 备案设置上次更改时间字段现在(做更新的时候)
- Set the CreatedOn/LastModified field for the record to now (when doing INSERT)
- 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();
不是这样的:
Not this:
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"?
我使用实体框架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端,我可以使用默认值,但将只对插入的(未更新的)。
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的(目前,我的仓库与泛型实现)。我需要做某种OO挂羊头卖狗肉的(如让我的POCO的实现一个接口,并调用一个方法)。我不adversed到这一点,但如果我要做到这一点,我宁愿手动设置的字段。
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或实体框架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的我想自动生成这些领域,我实现这个inteface。
For any POCO's i wish to auto-generate these fields, i implement this inteface.
使用我的问题的例子:
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.
}
我可能会打破code。在添加了以后于辅助/扩展方法。
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 - 如何自动更新上次更改为实体领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!