locked
DBContext.SaveChanges() doesnt take effect on Entity List Deleting RRS feed

  • Question

  • User62590854 posted

    Hello I have folowing problem that I cannot Understand:

    Premises:

    I'm working on a Blazor Project Updated at last Framework with EFCore 5.0.1 connected to PostgreSQL DB.

    The Calsses involved are following:

        public class TmkLayoutRow
        {
            public TmkLayoutRow()
            {
                LayoutColumns = new List<TmkLayoutColumn>();
            }
            public TmkLayoutRow(TmkLayoutRow row)
            {
                Index = row.Index;
                LayoutColumns = new();
                foreach (var col in row.LayoutColumns)
                {
                    LayoutColumns.Add(new(col));
                }
            }
    
            [Key]
            public int Id { get; set; }
    
            public int Index { get; set; }
    
            public int TmkLayoutId { get; set; }
            public TmkLayoutTemplate TmkLayout { get; set; }
    
            public List<TmkLayoutColumn> LayoutColumns { get; set; }
        }
    
    

    AND

        public class TmkLayoutColumn
        {
            public TmkLayoutColumn()
            {
    
            }
    
            public TmkLayoutColumn(TmkLayoutColumn col)
            {
                FieldName = col.FieldName;
                Index = col.Index;
                ColumnSize = col.ColumnSize;
            }
    
            [Key]
            public int Id { get; set; }
            [Required]
            public string FieldName { get; set; }
            public int Index { get; set; }
    
            [ColumnSizeRequired]
            public GridColSize ColumnSize { get; set; }
    
            public int LayoutRowId { get; set; }
            public TmkLayoutRow LayoutRow { get; set; }
        }
    

    Related with this code:

                builder.Entity<TmkLayoutRow>()
                    .HasMany(lyr => lyr.LayoutColumns)
                    .WithOne(lyc => lyc.LayoutRow)
                    .HasForeignKey(fk => fk.LayoutRowId)
                    .OnDelete(DeleteBehavior.Cascade);
    

    I update the table with following code using a repository:

            public async Task<bool> UpdateTmkLayoutTemplateAsync(TmkLayoutTemplate template)
            {
                var oldTemplate = await context.TmkLayoutTemplates.FirstOrDefaultAsync(tmp => tmp.Id == template.Id);
                if (oldTemplate == null)
                {
                    throw new Exception("NoTemplateToUpdateFoundWitSuchId");
                }
    
                oldTemplate.LayoutRows.RemoveRange(0, oldTemplate.LayoutRows.Count);
    
                foreach (var row in template.LayoutRows)
                {
                    oldTemplate.LayoutRows.Add(new(row));
                }
    
                var affected = await context.SaveChangesAsync();
    
                if (affected > 0)
                    return true;
                else 
                    return false;
            }
    

    The problem is that The "oldTemplate.LayoutRows.RemoveRange(0, oldTemplate.LayoutRows.Count);" doesnt take effect on DB side (Because on C# side The list are removed from LayoutRows),

    Otherwise  the rows adding are taking effects bought on c# side and on DB Side

    So at the end of saving I have the old Rows and the new too.

    I tried all but I cannot understand where is the problem

    Thankyou for helping

    Piercarlo

    Sunday, January 10, 2021 11:54 AM

Answers