开发者

Overriding Save method or adding Saving event with EF4

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜