Ends in:
0 DAY
22 HRS
27 MIN
28 SEC
Ends in:
0 D
22 H
27 M
28 S
Open Closed

Unintentional data changes #8197


User avatar
0
omer_yel created
  • ABP Framework version: v8.2.0
  • UI Type: Blazor Server
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): tiered

On appservice we are getting data as list. After getting list, we change property value using for loop. We get successfully excepted result on UI and in list. Additionally row that related that data at sql also changes.

public virtual async Task<PagedResultDto<CityWithNavigationPropertiesDto>> GetListAsync(GetCitiesInput input) { var totalCount = await _cityRepository.GetCountAsync(input.FilterText, input.Name, input.IsActive, input.CountryId); var items = await _cityRepository.GetListWithNavigationPropertiesAsync(input.FilterText, input.Name, input.IsActive, input.CountryId, input.Sorting, input.MaxResultCount, input.SkipCount);

var tmp = items.FirstOrDefault(); tmp.City.Name = "newValue";

return new PagedResultDto<CityWithNavigationPropertiesDto> { TotalCount = totalCount, Items = ObjectMapper.Map<List<CityWithNavigationProperties>, List<CityWithNavigationPropertiesDto>>(items) }; }

For test purposes, as you see above code we assing 'newValue' first item of list. It is okey, it changes. But it also changes database row of it. We also disable UOW, but result is same. On appservice we just get list , not create or update entity, despite this it updates value.

--------------------------------------------------------------------------------------------- By the way, we renewed our license but our ticket count did not renew to 30.


5 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Please try to map your entities to CityWithNavigationProperties then change the value in CityWithNavigationProperties.

    ObjectMapper.Map<List<CityWithNavigationProperties>, List<CityWithNavigationPropertiesDto>>(items)


    By the way, we renewed our license but our ticket count did not renew to 30.

    Please send an email to info@abp.io

  • User Avatar
    0
    omer_yel created

    hi We tried your method but it did not work. We also tried AsnoTracking method. It worked. As you see previous code block, we make just get process. So even if ef core tracks the entities, it should not change database value. Why database updates occur? To prevent data corruption should we always use AsNoTracking method while getting list? is there any performance effect using AsNoTracking on project.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Have you tried this?

    public virtual async Task<PagedResultDto<CityWithNavigationPropertiesDto>> GetListAsync(GetCitiesInput input)
    {
    	var totalCount = await _cityRepository.GetCountAsync(input.FilterText, input.Name, input.IsActive, input.CountryId);
    	var items = await _cityRepository.GetListWithNavigationPropertiesAsync(input.FilterText, input.Name, input.IsActive, input.CountryId, input.Sorting, input.MaxResultCount, input.SkipCount);
    
    	var dto = ObjectMapper.Map<List<CityWithNavigationProperties>, List<CityWithNavigationPropertiesDto>>(items);
    
    	var tmp = dto.FirstOrDefault();
    	tmp.City.Name = "newValue";
    
    	return new PagedResultDto<CityWithNavigationPropertiesDto>
    	{
    		TotalCount = totalCount,
    		Items = dto
    	};
    }
    

    Of course if you don't want to change the entities, You can inject the Read-Only Repositories

    see https://github.com/abpframework/abp/pull/17421

  • User Avatar
    0
    omer_yel created

    When mapping to CityWithNavigationPropertiesDto, it worked. Also using AsNoTracking prevented changing. Thanks for reply.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    great

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19