public async Task <DBDatafileModel> UpsertAsync(DBDatafileModel dataFile) { var existing = await _db.Datafile.FirstOrDefaultAsync(); if (null == existing) { _db.ChangeTracker.TrackGraph(dataFile, node => node.Entry.State = !node.Entry.IsKeySet ? EntityState.Added : EntityState.Unchanged); _db.Datafile.Add(dataFile); } else { _db.Entry(existing).CurrentValues.SetValues(dataFile); } await _db.SaveChangesAsync(); return(dataFile); }
/// <summary> /// Checks and reads the current local datafile /// </summary> /// <param name="dbDatafile"></param> private async void CheckLocalDatafile(DBDatafileModel dbDatafile) { try { ObservableCollection <TwoFACodeModel> deserializeCollection = new ObservableCollection <TwoFACodeModel>(); StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(dbDatafile.Path); if (await FileService.FileExistsAsync(dbDatafile.Name, folder)) { var dbHash = await App.Repository.Password.GetAsync(); // prevent write of the datafile _initialization = true; try { string datafileStr = await FileService.ReadStringAsync(dbDatafile.Name, folder); if (!string.IsNullOrEmpty(datafileStr)) { // read the iv for AES DatafileModel datafile = NewtonsoftJSONService.Deserialize <DatafileModel>(datafileStr); var iv = datafile.IV; datafile = NewtonsoftJSONService.DeserializeDecrypt <DatafileModel>( SecretService.Helper.ReadSecret(Constants.ContainerName, dbHash.Hash), iv, datafileStr); deserializeCollection = datafile.Collection; } } catch (Exception) { ShowPasswordError(); } } // file not found case else { ShowFileOrFolderNotFoundError(); } if (deserializeCollection != null) { Collection.AddRange(deserializeCollection); if (Collection.Count == 0) { // if no error has occured if (!_errorOccurred) { EmptyAccountCollectionTipIsOpen = true; } } else { if (EmptyAccountCollectionTipIsOpen) { EmptyAccountCollectionTipIsOpen = false; } } } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception exc) #pragma warning restore CA1031 // Do not catch general exception types { if (exc is UnauthorizedAccessException) { ShowUnauthorizedAccessError(); } else if (exc is FileNotFoundException) { ShowFileOrFolderNotFoundError(); } else { _errorOccurred = true; TrackingManager.TrackException(exc); ErrorDialogs.ShowUnexpectedError(exc); } } // writing the data file is activated again _initialization = false; }