Base solution for your next web application
Open Closed

Entity History is not tracking updates #8207


User avatar
0
fawad29 created

Hi,

I am following the link displayed below for implementing entity history.

https://docs.aspnetzero.com/en/aspnet-core-mvc/latest/Features-Mvc-Core-Entity-History

I have an entity class which has some properties. When I update this entity, audit logs does not show old (previous) value, it only shows the new i.e. updated values. For example, if my entity has a property called Name and I update its value from UserA to UserB, the audit log does not show UserA as old Values.

Entity is shown below

EntityFrameworkCoreModule is shown below

EntityHistoryHelper code is shown below:

Can you please help?

Thanks


8 Answer(s)
  • User Avatar
    0
    musa.demir created

    Hi @learner29

    • What is your product version?
    • What is ABP Framework version?
    • What is product framework type (.net framework or .net core)?
  • User Avatar
    0
    fawad29 created

    Hi @demirmusa,

    Product version is 8.0.0. ABP Framework is the one which came with version 8.0.0. ASP.NET Core MVC & jQuery Project

  • User Avatar
    0
    maliming created
    Support Team

    hi @learner29

    Please share the code of your entity class and update entity methods.

  • User Avatar
    0
    fawad29 created

    Hi @maliming,

    Please see below the required information.

    [Table("Ethnicities")]
        //[Audited]
        public class Ethnicity :  FullAuditedEntity, IMustHaveTenant 
        {
            public Ethnicity()
            {
                Clients = new HashSet<Client>();
            }
            [MinLength(EthnicityConsts.MinCodeLength)]
            [MaxLength(EthnicityConsts.MaxCodeLength)]
            [Required]
           // [Audited]
            public string Code { get; set; }
            [MinLength(EthnicityConsts.MinTextLength)]
            [MaxLength(EthnicityConsts.MaxTextLength)]
            [Required]
           // [Audited]
            public string Text { get; set; }
            //[Audited]
            public virtual int TenantId { get; set; }
            [Required]
            public int DisplayOrder { get; set; }
    
            public virtual ICollection<Client> Clients { get; set; }
    
        }
    

    App service is displayed below.

    //[DisableAuditing]
        [AbpAuthorize(AppPermissions.Pages_Administration_Ethnicities)]
       // [Audited]
        public class EthnicityAppService : MyAppServiceBase, IEthnicityAppService
        {        
            private readonly IRepository<Ethnicity> _ethnicityPLRepositroy;        
            public EthnicityAppService(IRepository<Ethnicity> ethnicityPLRepositroy)
            {
                _ethnicityPLRepositroy = ethnicityPLRepositroy;
            }        
            [AbpAuthorize(AppPermissions.Pages_Administration_Ethnicities)]
            public async Task<ListResultDto<EthnicitiesListDto>> GetEthnicity(GetEthnicityInput getEthnicityInput)
            {
               
                var ethnicity =  _ethnicityPLRepositroy.GetAll()
                                                 .WhereIf(
                                                    !getEthnicityInput.Filter.IsNullOrWhiteSpace(),
                                                    filter =>
                                                        (filter.Code.Contains(getEthnicityInput.Filter) || filter.Code.StartsWith(getEthnicityInput.Filter)) ||
                                                        (filter.Text.Contains(getEthnicityInput.Filter) || filter.Text.StartsWith(getEthnicityInput.Filter))
                                                 );
                var ethnicityCount = await ethnicity.CountAsync();
                ethnicity = ethnicity.OrderBy(getEthnicityInput.Sorting).PageBy(getEthnicityInput);
                return new PagedResultDto<EthnicitiesListDto>(
                    ethnicityCount,
                    ObjectMapper.Map<List<EthnicitiesListDto>>(ethnicity)
                    );
            }
            
            [AbpAuthorize(AppPermissions.Pages_Administration_Ethnicities_Create)]
            public async Task Create(CreateEthnicityInputDto createEthnicityPLInputDto)
            {
                var ethnicityEntity = ObjectMapper.Map<Ethnicity>(createEthnicityPLInputDto);
                ethnicityEntity.TenantId = (Int32)AbpSession.TenantId;
                var displayOrder = _ethnicityPLRepositroy
                    .GetAll().Where(x => x.IsDeleted == false & x.TenantId == ethnicityEntity.TenantId)
                    .Select(x => x.DisplayOrder)
                    .Max();
    
                ethnicityEntity.DisplayOrder = displayOrder + 1;
                var result = await _ethnicityPLRepositroy.InsertAsync(ethnicityEntity);
            }
           
            public async Task<GetEthnicityEditOutput> GetById(int id)
            {
                var ethnicityPLEntity = await _ethnicityPLRepositroy.GetAsync(id);
                var ethnicityPLEditDto = ObjectMapper.Map<EthnicityEditDto>(ethnicityPLEntity);
                var getEthnicityPLEditOutput = new GetEthnicityEditOutput();
                getEthnicityPLEditOutput.EthnicityEditDto = ethnicityPLEditDto;
                return getEthnicityPLEditOutput;
            }
           
            [AbpAuthorize(AppPermissions.Pages_Administration_Ethnicities_Edit)]
            public async Task Update(UpdateEthnicityInputDto updateEthnicityPLInputDto)
            {
               
                var ethnicityPLEntity = ObjectMapper.Map<Ethnicity>(updateEthnicityPLInputDto);
                ethnicityPLEntity.TenantId = (Int32)AbpSession.TenantId;
                var result = await _ethnicityPLRepositroy.UpdateAsync(ethnicityPLEntity);
            }
            
            [AbpAuthorize(AppPermissions.Pages_Administration_Ethnicities_Delete)]
            public async Task Delete(UpdateEthnicityInputDto updateEthnicityPLInputDto)
            {
                    await _ethnicityPLRepositroy.DeleteAsync(updateEthnicityPLInputDto.Id);
            }
        }
    
  • User Avatar
    1
    maliming created
    Support Team

    hi @learner29

    try

    [AbpAuthorize(AppPermissions.Pages_Administration_Ethnicities_Edit)]
    public async Task Update(UpdateEthnicityInputDto updateEthnicityPLInputDto)
    {
    	var ethnicityPLEntity = await_ethnicityPLRepositroy.GetAsync(updateEthnicityPLInputDto.Id);
    	ObjectMapper.Map(updateEthnicityPLInputDto, ethnicityPLEntity);
    	ethnicityPLEntity.TenantId = (Int32)AbpSession.TenantId;
    	var result = await _ethnicityPLRepositroy.UpdateAsync(ethnicityPLEntity);
    }
    
    
  • User Avatar
    0
    fawad29 created

    Hi @maliming,

    I have followed your suggestion. I am now receiving following error:

    My update dto is as follows.

    Please advice.

  • User Avatar
    0
    musa.demir created

    You have missing mapping. Check https://aspnetboilerplate.com/Pages/Documents/Object-To-Object-Mapping#creating-mappings

  • User Avatar
    0
    maliming created
    Support Team

    hi @learner29

    The key point of the problem is that you need to query the entity first and then update it

    eg

    	var ethnicityPLEntity = await _ethnicityPLRepositroy.GetAsync(Id);
    	ethnicityPLEntity.Name = "123123";
    	await _ethnicityPLRepositroy.UpdateAsync(ethnicityPLEntity);