Entity Framework 4.1 (Code First) audit column
- by Ken Pespisa
I'm using Entity Framework 4.1 with a Code-First approach on an ASP.NET MVC site
Say I have an entity named Profile that keeps track of a user's favorite book, and I want to track when the user updates their favorite book.
UPDATED:
Using the class below as an example, I want to set the FavoriteBookLastUpdated property to the current date whenever the value of the FavoriteBook property changes.
public class Profile
{
public int Id { get; set; }
public string Name { get; set; }
public string FavoriteBook { get; set; }
public DateTime? FavoriteBookLastUpdated { get; set; }
}
Right now I just update that field, if appropriate, in the controller's Edit action before calling the DBContext's SaveChanges() method.
Is there a way I can put that logic in my model somehow? I'd prefer not to use triggers on the database side.