Last active
September 19, 2018 08:08
-
-
Save TomKearney/13db43cfe6594c39b5993b2d1717c5d5 to your computer and use it in GitHub Desktop.
Entity Framework - modifying an entity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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