mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
parent
d00f3e6a20
commit
c9628a2aef
1 changed files with 14 additions and 1 deletions
|
|
@ -30,11 +30,13 @@ public static void SeedOrUpdate(this FilterListsDbContext dbContext, string data
|
|||
dbContext.SeedOrUpdate<SoftwareSyntax>(dataPath);
|
||||
}
|
||||
|
||||
private static void SeedOrUpdate<TEntity>(this DbContext dbContext, string dataPath) where TEntity : IBaseEntity
|
||||
private static void SeedOrUpdate<TEntity>(this DbContext dbContext, string dataPath)
|
||||
where TEntity : class, IBaseEntity
|
||||
{
|
||||
var entityType = dbContext.Model.FindEntityType(typeof(TEntity));
|
||||
var properties = GetPropertiesLessValueGeneratedTimestamps(entityType);
|
||||
var seedRows = GetSeedRows<TEntity>(dataPath);
|
||||
Delete(dbContext, seedRows);
|
||||
InsertOnDuplicateKeyUpdate(dbContext, properties, entityType, seedRows);
|
||||
}
|
||||
|
||||
|
|
@ -57,6 +59,17 @@ private static List<TEntity> GetSeedRows<TEntity>(string dataPath) where TEntity
|
|||
}
|
||||
}
|
||||
|
||||
private static void Delete<TEntity>(DbContext dbContext, IEnumerable<TEntity> seedRows)
|
||||
where TEntity : class, IBaseEntity
|
||||
{
|
||||
var idProperties = typeof(TEntity).GetProperties().Where(p => p.Name.Contains("Id"));
|
||||
var toRemove = dbContext.Set<TEntity>()
|
||||
.Select(s => idProperties)
|
||||
.Except(seedRows.Select(s => idProperties));
|
||||
dbContext.RemoveRange(toRemove);
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private static void InsertOnDuplicateKeyUpdate<TEntity>(DbContext dbContext,
|
||||
IReadOnlyCollection<IProperty> properties, IEntityType entityType, IEnumerable<TEntity> seedRows)
|
||||
where TEntity : IBaseEntity
|
||||
|
|
|
|||
Loading…
Reference in a new issue