Base solution for your next web application
Open Closed

Error editing records in a multilingual table #12619


User avatar
0
MarturAbpUser created

Hello,

I am adding a record to my table, which has a multi-language option. When I then try to edit this record, I get the following error.

Could you help me?

And I added the Logs.txt onyl error code.

ERROR 2026-01-30 16:28:56,093 [44 ] Mvc.ExceptionHandling.AbpExceptionFilter - The association between entities 'DefectCharacteristic' and 'DefectCharacteristicTranslation' with the key value '{CoreId: 6}' has been severed, but the relationship is either marked as required or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, configure the relationship to use cascade deletes. System.InvalidOperationException: The association between entities 'DefectCharacteristic' and 'DefectCharacteristicTranslation' with the key value '{CoreId: 6}' has been severed, but the relationship is either marked as required or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, configure the relationship to use cascade deletes. at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.HandleConceptualNulls(Boolean sensitiveLoggingEnabled, Boolean force, Boolean isCascadeDelete) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.HandleNullForeignKey(IProperty property, Boolean setModified, Boolean isCascadeDelete) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean isMaterialization, Boolean setModified, Boolean isCascadeDelete, CurrentValueType valueType) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean isMaterialization, Boolean setModified, Boolean isCascadeDelete) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.set_Item(IPropertyBase propertyBase, Object value) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.ConditionallyNullForeignKeyProperties(InternalEntityEntry dependentEntry, InternalEntityEntry principalEntry, IForeignKey foreignKey) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.NavigationCollectionChanged(InternalEntityEntry entry, INavigationBase navigationBase, IEnumerable1 added, IEnumerable1 removed) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectNavigationChange(InternalEntityEntry entry, INavigationBase navigationBase) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.LocalDetectChanges(InternalEntityEntry entry) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectChanges(IStateManager stateManager) at Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.Entries() at Abp.Zero.EntityFrameworkCore.AbpZeroCommonDbContext`3.SaveChangesAsync(CancellationToken cancellationToken) at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.SaveChangesAsync() at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.CompleteUowAsync() at Abp.Domain.Uow.UnitOfWorkBase.CompleteAsync() at Abp.AspNetCore.Mvc.Uow.AbpUowActionFilter.OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

3 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @MarturAbpUser

    There is a relationship between DefectCharacteristic (parent) and DefectCharacteristicTranslation (child).

    In the DefectCharacteristicTranslation table, CoreId is used as a foreign key and it is not nullable.

    In the Update method, defectCharacteristic.Translations.Clear() is called. This operation removes the child entities from the collection, but EF Core cannot delete them from the database because cascade delete is not configured.

    You should update the Update method as follows:

    [AbpAuthorize(AppPermissions.Pages_DefectCharacteristics_Edit)]
    protected virtual async Task Update(CreateOrEditDefectCharacteristicDto input)
    {
        var defectCharacteristic = await _defectCharacteristicRepository
            .GetAllIncluding(x => x.Translations)
            .FirstOrDefaultAsync(x => x.Id == (int)input.Id);
    
        // First, delete existing translations from the database
        foreach (var translation in defectCharacteristic.Translations.ToList())
        {
            await _defectCharacteristicTranslationRepository.DeleteAsync(translation);
        }
    
        // Then clear the collection
        defectCharacteristic.Translations.Clear();
    
        ObjectMapper.Map(input, defectCharacteristic);
    }
    

    Configure Cascade Delete

    You can configure the relationship as cascade delete in the DbContext:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    
        //...
        
        modelBuilder.Entity<DefectCharacteristic>()
            .HasMany(x => x.Translations)
            .WithOne()
            .HasForeignKey(x => x.CoreId)
            .OnDelete(DeleteBehavior.Cascade);
    }
    

    After that, you need to add a migration and update the database.

    If the issue persists after applying these steps, please do not hesitate to contact us.

    Thank you.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    MarturAbpUser created

    Hello, I changed it as you said. I deleted the old records from the database. But I didn't add the new things I entered.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @MarturAbpUser When you apply the fix mentioned in my previous message, do you encounter any errors during the update process? If there is an error after applying the fix, could you please share the error details or logs? If there is no error but you are experiencing behavior that does not align with the expected business logic, could you please explain the issue in detail, including the steps you followed and the expected vs. actual outcome? We would be happy to assist you further. Thank you.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)