If I have the following entity:
public class PocoWithDates {
public string PocoName { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
}
corresponding to an SQL Server 2008 (R2) table with the same name/attributes...
How can I automatically:
- Set the DateCreated
and DateUpdated
field to now when doing an INSERT
- Set the DateUpdated
field to now when doing an 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"? I'm using Entity Framework 6.0. Is there a way that EF can do that automatically? I know MySQL can do something like this. Or using triggers/stored procedures in SQL Server. But I do NOT want that.
This question is an alternative and altered version of this question: Entity Framework/SQL2008 - How to Automatically Update LastModified fields for Entities?
This question got some great solutions/answers for EF 4.0. But I want it for EF6.0 with a DBContext
. If I had more time I would try and adapt those solutions and get them working in EF6.0. But for now I'm wondering if anybody solved this already, or knows how to transcribe the custom SaveChanges()
or Add()
overrides for EF4.0 to EF6.0.