UnitOfWork
UnitOfWork copied to clipboard
AutoHistory - before and after are same in changed column
Hello,
I m trying to add autohistory to my project and everything is looking good except the changed column in AutoHistory table in the database. In changed column, before and after parts are same and you can see an example below.
My Project Source Codes https://github.com/mehmetalierol/Net-Core-2-1-WebApi
Example: {" before":{"CreateDate":"2018-09-28T18:01:43.0946195Z","Creator":null,"MailAdress":"[email protected]","Name":"456456456","OrganizationId":"b57c7cab-3eeb-436c-8515-934d9c9fd055","PasswordHash":null,"Phone":"05554443322","Status":1,"Surname":"456456"}," after":{"CreateDate":"2018-09-28T18:01:43.0946195Z","Creator":null,"MailAdress":"[email protected]","Name":"456456456","OrganizationId":"b57c7cab-3eeb-436c-8515-934d9c9fd055","PasswordHash":null,"Phone":"05554443322","Status":1,"Surname":"456456 "}}
before
's value wrong or after
's value wrong?
The wrong one is before part. Its not indicates the previous data in my table only shows the new value like after part does.
OK, maybe the DbContext is disconnected.
I didn't figure it out :( I m missing a point but i couldn't find it.
My repository update method: public T Update(T entityToUpdate) { _dbset.Update(entityToUpdate); return entityToUpdate; }
My unitofwork savechanges method: public int SaveChanges(bool ensureAutoHistory = false) { var transaction = _transation != null ? _transation : _context.Database.BeginTransaction(); using (transaction) { try { //Context boş ise hata fırlatıyoruz if (_context == null) { throw new ArgumentException("Context is null"); }
if (ensureAutoHistory)
{
_context.EnsureAutoHistory();
}
//Save changes metodundan dönen int result ı yakalayarak geri dönüyoruz.
int result = _context.SaveChanges();
//Sorun yok ise kuyruktaki tüm işlemleri commit ederek bitiriyoruz.
transaction.Commit();
return result;
}
catch (Exception ex)
{
//Hata ile karşılaşılır ise işlemler geri alınıyor
transaction.Rollback();
throw new Exception("Error on save changes ", ex);
}
}
}
ControllerBase class update method: [HttpPost("Update")] public virtual ApiResult<TDto> Update([FromBody] TDto item) { var resolvedItem = String.Join(',', item.GetType().GetProperties().Select(x => $" - {x.Name} : {x.GetValue(item)} - ").ToList()); try { var TResult = _repository.Update(Mapper.Map<T>(item)); _logger.LogInformation($"Update record to the {typeof(T)} table. Data:{resolvedItem}"); return new ApiResult<TDto> { StatusCode = StatusCodes.Status200OK, Message = "Success", Data = Mapper.Map<T, TDto>(TResult) }; } catch (Exception ex) { _logger.LogError($"Update record error to the {typeof(T)} table. Data: {resolvedItem} exception:{ex}"); return new ApiResult<TDto> { StatusCode = StatusCodes.Status500InternalServerError, Message = $"Error:{ex.Message}", Data = null }; } }
And Controller overrided update method: public override ApiResult<CustomerDto> Update([FromBody] CustomerDto item) { var result = base.Update(item); _uow.SaveChanges(true); return result; }
Again if you want to see whole code you can find it with the link below. https://github.com/mehmetalierol/Net-Core-2-1-WebApi
I had this issue and found out that I was using two different DbContext...
OK, maybe the DbContext is disconnected.
Is there any solution for DisconnectedDbContext?
Having the same issue! Could someone point out what the best praktisch would be for a disconnected environment?? because isn't that the case in most real live scenario's??!
I am having the same problem as well... the before value its the same of after... And i am using the same DbContext.