UnitOfWork icon indicating copy to clipboard operation
UnitOfWork copied to clipboard

AutoHistory - before and after are same in changed column

Open mehmetalierol opened this issue 6 years ago • 8 comments

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 "}}

mehmetalierol avatar Sep 28 '18 18:09 mehmetalierol

before's value wrong or after's value wrong?

rigofunc avatar Sep 29 '18 02:09 rigofunc

The wrong one is before part. Its not indicates the previous data in my table only shows the new value like after part does.

mehmetalierol avatar Sep 29 '18 06:09 mehmetalierol

OK, maybe the DbContext is disconnected.

rigofunc avatar Sep 29 '18 06:09 rigofunc

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

mehmetalierol avatar Sep 29 '18 08:09 mehmetalierol

I had this issue and found out that I was using two different DbContext...

ccarrero301 avatar Jan 19 '19 04:01 ccarrero301

OK, maybe the DbContext is disconnected.

Is there any solution for DisconnectedDbContext?

canperk avatar Mar 14 '19 08:03 canperk

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??!

roscoedeemarutam avatar May 13 '19 08:05 roscoedeemarutam

I am having the same problem as well... the before value its the same of after... And i am using the same DbContext.

Alu1994 avatar Jul 27 '19 19:07 Alu1994