diff --git a/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs b/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs index 1ee28876c..27664536d 100644 --- a/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs +++ b/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Linq.Expressions; @@ -16,6 +17,9 @@ public static class SeedFilterListsDbContext { public static async Task SeedOrUpdateAsync(FilterListsDbContext dbContext, string dataPath) { + if (dbContext is null) + throw new ArgumentNullException(nameof(dbContext)); + await SeedOrUpdate(dbContext, dataPath); await SeedOrUpdate(dbContext, dataPath); await SeedOrUpdate(dbContext, dataPath); @@ -94,7 +98,7 @@ private static async Task InsertOnDuplicateKeyUpdate(DbContext dbContex var entityType = dbContext.Model.FindEntityType(typeof(TEntity)); var properties = GetPropertiesLessValueGeneratedTimestamps(entityType); var values = CreateValues(seed, properties); - if (values == "") + if (string.IsNullOrEmpty(values)) return; var columns = string.Join(", ", properties.Select(x => x.Name)); var updates = CreateUpdates(properties); @@ -114,17 +118,18 @@ private static List GetPropertiesLessValueGeneratedTimestamps(IEntity }.Contains(x.Name)) .ToList(); - private static string CreateValues(IEnumerable seed, - IReadOnlyCollection properties) where TEntity : IBaseEntity => - seed.Select(row => CreateRowValues(properties, row)) - .Aggregate("", (current, rowValues) => current == "" ? rowValues : current + ", " + rowValues); + private static string + CreateValues(IEnumerable seed, IReadOnlyCollection properties) + where TEntity : IBaseEntity => seed.Select(row => CreateRowValues(properties, row)) + .Aggregate("", + (current, rowValues) => string.IsNullOrEmpty(current) ? rowValues : current + ", " + rowValues); private static string CreateRowValues(IEnumerable properties, TEntity row) where TEntity : IBaseEntity => (from property in properties let value = row.GetType().GetProperty(property.Name)?.GetValue(row) select FormatDataForMySql(property, value)).Aggregate("", - (rowValues, value) => rowValues == "" ? "(" + value : rowValues + ", " + value) + ")"; + (rowValues, value) => string.IsNullOrEmpty(rowValues) ? "(" + value : rowValues + ", " + value) + ")"; private static object FormatDataForMySql(IProperty property, object value) { @@ -133,9 +138,9 @@ private static object FormatDataForMySql(IProperty property, object value) if (property.ClrType == typeof(string)) return "'" + value.ToString().Replace("'", "''") + "'"; if (property.ClrType == typeof(bool)) - return Convert.ToInt32(value); + return Convert.ToInt32(value, CultureInfo.InvariantCulture); if (property.ClrType == typeof(DateTime?)) - return "'" + ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss") + "'"; + return "'" + ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) + "'"; return value; } @@ -145,8 +150,9 @@ private static string CreateUpdates(IReadOnlyCollection properties) (from property in properties where !property.IsPrimaryKey() select property.Name + " = VALUES(" + property.Name + ")").Aggregate("", - (updates, columnUpdates) => updates == "" ? columnUpdates : updates + ", " + columnUpdates); - if (update == "") + (updates, columnUpdates) => + string.IsNullOrEmpty(updates) ? columnUpdates : updates + ", " + columnUpdates); + if (string.IsNullOrEmpty(update)) update = GetUpdateUnchangedColumnHack(properties); return update; }