private static void ImportData() { DataBaseContents data = JsonConvert.DeserializeObject <DataBaseContents>(File.ReadAllText(@"d:\Database.json")); var dataContext = new DataContext(); foreach (var dataUser in data.Users) { dataContext.Users.Add(dataUser); } dataContext.SaveChanges(); foreach (var dataEntry in data.Entries) { dataContext.Entries.Add(dataEntry); } foreach (var processingRule in data.Rules) { dataContext.ProcessingRules.Add(processingRule); } // Clear all references to other objects. The transaction need to be reprocessed after import. foreach (var dataTransaction in data.Transactions) { dataTransaction.EntryPeriodKey = null; dataContext.Transactions.Add(dataTransaction); } dataContext.SaveChanges(); }
/// <summary>Export the data in a JSON format to a file specified in fileName. /// </summary> /// <param name="fileName">Name of the file.</param> public void Export(string fileName) { var database = new DataBaseContents { Users = new List <User> { mAuthorizationService.CurrentUser }, Entries = mEntryDataService.GetAll(), Rules = mProcessingRuleDataService.GetAll(), Transactions = mTransactionDataService.GetAll() }; // Clear all child entries to prevent circulair reference when exporting to JSON. foreach (var databaseEntry in database.Entries) { databaseEntry.ChildEntries?.Clear(); databaseEntry.ParentEntry = null; } using (var file = File.CreateText(fileName)) { var serializer = new JsonSerializer(); serializer.Serialize(file, database); } }