Update migration add data truncation if source data exceeds length (#190)

This commit is contained in:
Leendert de Borst
2024-12-25 22:41:56 +01:00
parent bf40539e92
commit a75d5c7a34

View File

@@ -226,7 +226,34 @@ public static partial class Program
if (items.Count > 0)
{
const int batchSize = 30;
// Get entity type from the model to check annotations
var entityType = destinationContext.Model.FindEntityType(typeof(T));
foreach (var item in items)
{
// Check each property for MaxLength annotation
foreach (var property in entityType!.GetProperties())
{
// Only process string properties
if (property.ClrType == typeof(string))
{
var maxLength = property.GetMaxLength();
if (maxLength.HasValue)
{
var propertyInfo = typeof(T).GetProperty(property.Name);
var value = propertyInfo?.GetValue(item) as string;
if (value?.Length > maxLength.Value)
{
propertyInfo?.SetValue(item, value.Substring(0, maxLength.Value));
Console.WriteLine($"Truncated {property.Name} in {tableName} from {value.Length} to {maxLength.Value} characters");
}
}
}
}
}
const int batchSize = 50;
foreach (var batch in items.Chunk(batchSize))
{
try