'ABP Update child and parent table in the same function
I have a code in .net core 6.0, abp 6.0, trying to make a small web system. I have a table named darbanPrisonerEntry Darban Prisoner Entry Model
public class DarbanPrisonerEntry : FullAuditedEntity<long>
{
public long BiometricId { get; set; }
public virtual BiometricInfoBase Biometric { get; set; }
public string FullName { get; set; }
public DateTime AdmissionDate { get; set; }
public string FatherName { get; set; }
public bool Prisoner { get; set; }
public string CNIC { get; set; }
public string FIRNo { get; set; }
public long PrisonId { get; set; }
public List<DabanPrisonerUnderSections> UnderSections { get; set; }
public long PoliceStationId { get; set; }
public bool PrisonerAdded { get; set; }
}
with a linked table named BiometricInfoBase Biometric Info Model
public class BiometricInfoBase : FullAuditedEntity<long>
{
public string FrontPic { get; set; }
public string LeftPic { get; set; }
public string RightPic { get; set; }
public string RightThumb { get; set; }
public string LeftThumb { get; set; }
public string RightIndex { get; set; }
public string LeftIndex { get; set; }
}
and a child table DabanPrisonerUnderSections Daban Prisoner Under Sections
public class DabanPrisonerUnderSections : FullAuditedEntity<long>
{
public long SectionId { get; set; }
public long DarbanPrisonerEntryId { get; set; }
public virtual DarbanPrisonerEntry DarbanPrisonerEntry { get; set; }
}
When i try insert command it works perfectly, it inserts entries in both the linked tables. Insert function
public async Task<CreateEditDarbanPrisonerEntryOutputDto> CreateUpdateDarbanPrisonerEntry(CreateEditDarbanPrisonerEntryInputDto input)
{
CreateEditDarbanPrisonerEntryOutputDto response = new CreateEditDarbanPrisonerEntryOutputDto();
try
{
using (_unitOfWorkManager.Current.SetTenantId(AbpSession.TenantId.GetValueOrDefault()))
{
if (AbpSession.UserId != null)
{
var darban = _objectMapper.Map<DarbanPrisonerEntry>(input.Data);
darban.Id= (await _darbanEntryRepo.InsertAndGetIdAsync(darban));
response.IsSuccessful = true;
response.Data=_objectMapper.Map<DarbanPrisonerEntryDto>(darban);
}
else
{
response.IsSuccessful = false;
throw new UserFriendlyException("User not logged in");
}
}
}
catch (Exception ex)
{
throw new UserFriendlyException(ex.InnerException==null ? ex.Message : ex.InnerException.Message);
}
return response;
}
but on update, it only updates the parent table and the linked table isnt updated and the child table gets multiple inserts. Update function
public async Task<CreateEditDarbanPrisonerEntryOutputDto> CreateUpdateDarbanPrisonerEntry(CreateEditDarbanPrisonerEntryInputDto input)
{
CreateEditDarbanPrisonerEntryOutputDto response = new CreateEditDarbanPrisonerEntryOutputDto();
try
{
using (_unitOfWorkManager.Current.SetTenantId(AbpSession.TenantId.GetValueOrDefault()))
{
if (AbpSession.UserId != null)
{
var darban = _objectMapper.Map<DarbanPrisonerEntry>(input.Data);
darban= (await _darbanEntryRepo.UpdateAsync(darban));
response.IsSuccessful = true;
response.Data=_objectMapper.Map<DarbanPrisonerEntryDto>(darban);
}
else
{
response.IsSuccessful = false;
throw new UserFriendlyException("User not logged in");
}
}
}
catch (Exception ex)
{
throw new UserFriendlyException(ex.InnerException==null ? ex.Message : ex.InnerException.Message);
}
return response;
}
even when I try to update the linked table separately enter image description here
public async Task<CreateEditDarbanPrisonerEntryOutputDto> CreateUpdateDarbanPrisonerEntry(CreateEditDarbanPrisonerEntryInputDto input)
{
CreateEditDarbanPrisonerEntryOutputDto response = new CreateEditDarbanPrisonerEntryOutputDto();
try
{
using (_unitOfWorkManager.Current.SetTenantId(AbpSession.TenantId.GetValueOrDefault()))
{
if (AbpSession.UserId != null)
{
var darban = _objectMapper.Map<DarbanPrisonerEntry>(input.Data);
var biometric = _objectMapper.Map<BiometricInfoBase>(input.Data.Biometric);
biometric =await _biometricBaseRepo.UpdateAsync(biometric);
darban= (await _darbanEntryRepo.UpdateAsync(darban));
response.IsSuccessful = true;
response.Data=_objectMapper.Map<DarbanPrisonerEntryDto>(darban);
}
else
{
response.IsSuccessful = false;
throw new UserFriendlyException("User not logged in");
}
}
}
catch (Exception ex)
{
throw new UserFriendlyException(ex.InnerException==null ? ex.Message : ex.InnerException.Message);
}
return response;
}
i get this error error
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
