private static int InsertData <T, TKey>(LaGouContext dbContext, Func <LaGouContext, DbSet <T> > dbSetSelector, ICollection <T> data, Expression <Func <T, TKey> > keySelector) where T : class { if (data.Count == 0) { return(0); } var dbSet = dbSetSelector(dbContext); var keyHashSet = dbSet.Select(keySelector).ToHashSet(); var keyFunc = keySelector.Compile(); var newData = data.Where(p => !keyHashSet.Contains(keyFunc(p))); if (!newData.Any()) { return(0); } dbSet.AddRange(newData); int insertCount = 0; try { insertCount = dbContext.SaveChanges(); } catch (Exception ex) { ShowLogException(ex); } return(insertCount); }
private static int InsertData <T>(LaGouContext dbContext, Func <LaGouContext, DbSet <T> > dbSetSelector, ICollection <T> data) where T : class { var dbSet = dbSetSelector(dbContext); dbSet.AddRange(data); int insertCount = 0; try { insertCount = dbContext.SaveChanges(); } catch (Exception ex) { ShowLogError(ex.Message); } return(insertCount); }