Entity Framework 4.1 (Code First) audit column

Posted by Ken Pespisa on Stack Overflow See other posts from Stack Overflow or by Ken Pespisa
Published on 2011-06-21T19:40:08Z Indexed on 2011/06/22 0:23 UTC
Read the original article Hit count: 249

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.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about entity-framework