//todo: to be tested public static void SaveAs <T>(this IEnumerable <T> data, ConstructionDataContext context, string filename, FileType type, string description, Action <T> add, ILogPrint print = null, IShowProgress showProgress = null) where T : class, IFile { var file = new File { FileName = filename, Type = type, Description = description }; var id = file.Add(context); print?.PrintLog("Start add item"); showProgress?.SetMaxValue(data.Count()); int step = 0; foreach (var item in data) { item.FileId = id; add.Invoke(item); step++; showProgress?.SetCurrentValue(step); } context.SaveChanges(); showProgress?.Done(); }
public void Add(T item) { try { AddItem(item); if (SaveChangesImmediately) { Context.SaveChanges(); } } catch (Exception exception) { if (exception.InnerException != null) { throw new Exception($"add {EntityName} failed, {exception.InnerException.Message}"); } } }
public static void SaveWithFileId <T>(this IEnumerable <T> data, ConstructionDataContext context, int fileId, ILogPrint print = null, IShowProgress showProgress = null) where T : class, IFile { print?.PrintLog($"The file id is {fileId}"); showProgress?.SetMaxValue(data.Count()); int i = 0; foreach (var item in data) { item.FileId = fileId; context.Entry(item).State = EntityState.Modified; showProgress?.SetCurrentValue(i); i++; } print?.PrintLog("Saving data to database."); context.SaveChanges(); showProgress?.Done(); }
public static bool SaveAs <T>(this IEnumerable <T> data, ConstructionDataContext context, string filename, out int existsFileId, string description, ILogPrint print = null, IShowProgress showProgress = null) where T : class, IFile { existsFileId = 0; var enumerable = data as T[] ?? data.ToArray(); var type = ConvertFileType(GetName(enumerable.First())); print?.PrintLog("Find the existing files."); var file = File.Select(context, filename, type); if (file != null) { existsFileId = file.Id; return(false); } file = new File { FileName = filename, Description = description, Type = type }; var id = file.Add(context); print?.PrintLog($"The file id is {id}"); showProgress?.SetMaxValue(enumerable.Length); int i = 0; foreach (var item in enumerable) { showProgress?.SetCurrentValue(i); item.FileId = id; i++; } print?.PrintLog("Saving data to database."); context.SaveChanges(); showProgress?.Done(); return(true); }