Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Update Entity with related entity ? Insert instead of Update. #7018


User avatar
0
uabel created

Hello, I have the following problem. I would like to update an entity incl. the attached list of objects. Unfortunately, the attached objects are always reinserted instead of being updated.

This is what my object looks like:

public class Template : Entity, IHasCreationTime, IHasModificationTime
    {
        public string Label { get; set; }
        public virtual TemplateType Type { get; set; }
        
        [JsonIgnore]
        public List<TemplateI18N> TemplateI18Ns { get; set; }
        public DateTime CreationTime { get; set; }
        public DateTime? LastModificationTime { get; set; }
    }

For the template i18n's an insert is made instead of an update.

This is my update function in the APP service:

protected virtual async Task UpdateTemplateAsync(CreateOrUpdateDto input)
        {
            var template = await templateRepository.GetAsync(input.Template.Id);
            input.Template.MapTo(template);
            template.LastModificationTime = DateTime.Now;
            await templateRepository.UpdateAsync(template);
        }_

The template object looks like this before the update:

DB looks like this befor update:

And after Update:

What am I doing wrong?`

Thanks for your help. Uwea


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

    You need to handle TemplateI18Ns separately. Add or update or delete it.

  • User Avatar
    0
    uabel created

    Hello, as in the following code it works. Is there an easier way?

     protected virtual async Task UpdateTemplateAsync(CreateOrUpdateDto input)
            {
                foreach (var id18n in input.Template.TemplateI18Ns)
                {
                    var thisI18N = _templateI18NRepository.Get(id18n.Id);
                    id18n.MapTo(thisI18N);
                    _templateI18NRepository.Update(thisI18N);
                    await CurrentUnitOfWork.SaveChangesAsync();
                }
                input.Template.TemplateI18Ns = null;
                var template = _templateRepository.Get(input.Template.Id);
                input.Template.MapTo(template);
                template.TemplateI18Ns = _templateI18NRepository.GetAll().Where(r => r.TemplateId == input.Template.Id).ToList();
                template.LastModificationTime = DateTime.Now;
                _templateRepository.Update(template);
                await CurrentUnitOfWork.SaveChangesAsync();
            }
    

    Uwe

  • User Avatar
    0
    maliming created
    Support Team

    You can refer to: https://github.com/aspnetzero/aspnet-zero-core/blob/48616521f7cfb2c5ca08490c3ed41f23a34d32ac/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Application/Authorization/Users/UserAppService.cs#L249

    Update the template first using Map (ignoring the TemplateI18Ns mapping)

    Then update TemplateI18Ns.

  • User Avatar
    0
    uabel created

    Sorry Unfortunately I get a 404 at the address https://github.com/aspnetzero/aspnet-zero-core/blob/48616521f7cfb2c5ca08490c3ed41f23a34d32ac/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Application/Authorization/Users/UserAppService.cs#L249

    maybe you can copy the code block in here

  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, the link isnt broken. you will need to login to your authorized github account as configured at https://aspnetzero.com/LicenseManagement

    See https://support.aspnetzero.com/QA/Questions/6981#answer-531b37e4-0fd2-a831-7197-39edb161b46e