Skip to content

Instantly share code, notes, and snippets.

@TomKearney
Last active September 19, 2018 08:08
Show Gist options
  • Select an option

  • Save TomKearney/13db43cfe6594c39b5993b2d1717c5d5 to your computer and use it in GitHub Desktop.

Select an option

Save TomKearney/13db43cfe6594c39b5993b2d1717c5d5 to your computer and use it in GitHub Desktop.
Entity Framework - modifying an entity
public AccountDo ModifyAccount(AccountDo account)
{
try
{
using (var dbContext = _dbContextFactory.Create())
{
dbContext.Accounts.Attach(Mapper.Map<Account>(account));
dbContext.SaveChanges(); // in this method, as the dirty objects should be flushed on dbContext.Dispose()
} // if we want to log the previous state, intercept the dbcontext methods: https://docs.microsoft.com/en-us/ef/ef6/fundamentals/logging-and-interception
return account;
}
catch (Exception e)
{
_logger.Error($"Db: Unable to modify account Details={account.ToDebugLog()}", e);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment