Starts in:
1 DAY
18 HRS
43 MIN
35 SEC
Starts in:
1 D
18 H
43 M
35 S
Open Closed

Automatic update for list properties in updateasync #1110


User avatar
0
l.lemmens created
  • ABP Framework version: v4.2.2
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

We have an object named 'trading company', this object contains a list of 'trading company dynamic fields'

public class TradingCompany : FullAuditedEntity<Guid>
    {
       ///other props

        /// <summary>
        /// DynamicFields.
        /// </summary>
        public IEnumerable<TradingCompanyDynamicField> Dynamicfields { get; set; }
    }

 public class TradingCompanyDynamicField: Entity<Guid>
    {
        /// <summary>
        /// DynamicField.
        /// </summary>
        public string DynamicField { get; set; }
    }

In the trading company appservice we have to update all the dynamic fields manually

public virtual async Task<TradingCompanyDto> UpdateAsync(Guid id, TradingCompanyUpdateDto input)
        {
            var tradingCompany = _tradingCompanyRepository.AsQueryable()
                .Where(d => d.Id == id)
                .Include(d => d.Dynamicfields)
                .FirstOrDefault();

            var currentList = tradingCompany.Dynamicfields;

            ObjectMapper.Map(input, tradingCompany);
            tradingCompany = await _tradingCompanyRepository.UpdateAsync(tradingCompany);

            // Delete in current not in input.
            var toDelete = currentList.Where(c => currentList.Select(d => d.Id).ToList().IndexOf(c.Id) == -1);
            await _tradingCompanyDynamicFieldRepository.DeleteManyAsync(toDelete);

            // Add in input, not in current.
            var toAdd = currentList.Where(c => currentList.Select(d => d.Id).ToList().IndexOf(c.Id) == -1);
            await _tradingCompanyDynamicFieldRepository.InsertManyAsync(
               toAdd.Select(a => new TradingCompanyDynamicField
               {
                   DynamicField = a.DynamicField,
                   TradingCompanyId = tradingCompany.Id
               }));

            // Update others.
            var toUpdate = currentList.Where(c => currentList.Select(d => d.Id).ToList().IndexOf(c.Id) > -1);
            await _tradingCompanyDynamicFieldRepository.UpdateManyAsync(
               toUpdate.Select(u => ObjectMapper.Map(currentList.FirstOrDefault(i => i.Id == u.Id), u))
               );

            return ObjectMapper.Map<TradingCompany, TradingCompanyDto>(tradingCompany);
        }

What we want would be something like this, this would also update all dynamicfields automatically:

public virtual async Task<TradingCompanyDto> UpdateAsync(Guid id, TradingCompanyUpdateDto input)
        {
            var tradingCompany = _tradingCompanyRepository.AsQueryable()
                .Where(d => d.Id == id)
                .Include(d => d.Dynamicfields)
                .FirstOrDefault();

            ObjectMapper.Map(input, tradingCompany);
            tradingCompany = await _tradingCompanyRepository.UpdateAsync(tradingCompany);

            return ObjectMapper.Map<TradingCompany, TradingCompanyDto>(tradingCompany);
        }

Executing the desired code results in swagger to the expected data. For example, update a trading company having 3 dynamic fields to one having only 2, returns an object only containing the 2 dynamic fields. But when we get by id/list or check in the database, we'll see all dynamic fields have the property 'isdeleted' set to true.

How can we achieve this?

Kind regards Lotte


4 Answer(s)
  • User Avatar
    0
    alper created
    Support Team Director

    I'll discuss this request with the team. we need to think if there's a drawback of this operation.

  • User Avatar
    0
    l.lemmens created

    Hello

    We're about a month further. Has this been discussed yet? Or could I possibly get an indication when this will be discussed?

    Kind regards Lotte

  • User Avatar
    0
    hikalkan created
    Support Team Co-Founder

    Hi,

    This is not a feature we are planning to add to the ABP Framework in a near time. However, the problem (if we say it is a problem) you have is a very commonly discussed (example) topic by the EF developers. You can google it like "ef core graph update". You will find some good solutions.

    Best regards.

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06