diff --git a/DiunaBI.Plugins.PedrolloPL/Importers/PedrolloPLImportP2.cs b/DiunaBI.Plugins.PedrolloPL/Importers/PedrolloPLImportP2.cs index 0bdedae..ade098b 100644 --- a/DiunaBI.Plugins.PedrolloPL/Importers/PedrolloPLImportP2.cs +++ b/DiunaBI.Plugins.PedrolloPL/Importers/PedrolloPLImportP2.cs @@ -362,13 +362,24 @@ public class PedrolloPLImportP2 : BaseDataImporter _logger.LogDebug("{ImporterType}: Saving {RecordCount} records to layer {LayerId}", ImporterType, records.Count, importLayer.Id); - // Set LayerId for all records + // Delete any existing records for this layer (shouldn't be any, but just in case) + var toDelete = _db.Records.Where(x => x.LayerId == importLayer.Id).ToList(); + if (toDelete.Count > 0) + { + _logger.LogWarning("{ImporterType}: Found {ExistingCount} existing records for layer {LayerId}, removing them", + ImporterType, toDelete.Count, importLayer.Id); + _db.Records.RemoveRange(toDelete); + } + + // Set all required properties for each record foreach (var record in records) { record.LayerId = importLayer.Id; + record.CreatedById = importLayer.CreatedById; + record.ModifiedById = importLayer.ModifiedById; + _db.Records.Add(record); } - _db.Records.AddRange(records); _db.SaveChanges(); _logger.LogInformation("{ImporterType}: Successfully saved {RecordCount} records to layer '{LayerName}'", diff --git a/DiunaBI.UI.Shared/Pages/LayerDetailPage.razor.cs b/DiunaBI.UI.Shared/Pages/LayerDetailPage.razor.cs index 65abbef..361f59b 100644 --- a/DiunaBI.UI.Shared/Pages/LayerDetailPage.razor.cs +++ b/DiunaBI.UI.Shared/Pages/LayerDetailPage.razor.cs @@ -64,7 +64,7 @@ public partial class LayerDetailPage : ComponentBase if (layer != null && layer.Records != null) { - records = layer.Records; + records = layer.Records.OrderBy(r => r.Code).ToList(); CalculateDisplayedColumns(); CalculateValueSum(); BuildUserCache();