Whats an easy (less code) way to add business logic to a 'code first' model to execute before its saved to the database?
E.g. given the Albums model from the music store example
public class Album
{
public string Title { get; set; }
public Genre Genre { get; set;开发者_如何学Python }
public DateTime LastUpdated { get; private set;}
}
e.g. if we add a LastUpdated property to the example, how can we ensure it's automatically set if the entity is updated - perhaps on a save using someting like
private void album_OnSave()
{
this.LastUpdated = DateTime.Now;
}
LastUpdated has to be part of the model. For making sure the property is up to date I would use a repository and the functions that insert or update the album should set LastUpdated.
Another alternative is to create a trigger during db creation that updates the LastUpdated column.
- MSDN - EF4 How to: Execute Business Logic When Saving Changes
精彩评论