public void SaveSettings() { using (var context = new Settings(ConnectionString)) { if (!context.DatabaseExists()) { // create database if it does not exist context.CreateDatabase(); } var query = context.SettingsTable.Where(setting => setting.Id == 0); string s = null; s = UserPreference.PreferredAssociations.Aggregate(s, (current, preferredAssociation) => current + (preferredAssociation.In + ';')); var formatted = s != null ? s.Substring(0, s.Length - 1) : ""; SettingsTable settingUpdate = null; if (query.Any()) { settingUpdate = query.First(); } if (!query.Any()) { var sett = new SettingsTable { Associations = formatted, Filtering = Convert.ToString(UserPreference.IsFiltering), Id = 0, CacheDate = _cacheTime.ToString(new CultureInfo("nl-BE")), AccessKey = UserPreference.AccessKey, FbUserId = UserPreference.FbUserId, Name = UserPreference.Name }; context.SettingsTable.InsertOnSubmit(sett); } else { if (settingUpdate != null) { settingUpdate.Filtering = Convert.ToString(UserPreference.IsFiltering); settingUpdate.Associations = formatted; settingUpdate.CacheDate = _cacheTime.ToString(new CultureInfo("nl-BE")); settingUpdate.AccessKey = UserPreference.AccessKey; settingUpdate.FbUserId = UserPreference.FbUserId; settingUpdate.Name = UserPreference.Name; } } context.SubmitChanges(); } }
private void LoadSettings() { using (var context = new Settings(ConnectionString)) { if (!context.DatabaseExists()) { // create database if it does not exist try { context.CreateDatabase(); } catch (Exception e) { Console.WriteLine(e.Data); } } else { var query = context.SettingsTable.Where(setting => setting.Id == 0); if (!query.Any()) { SaveSettings(); return; } var sett = query.First(); UserPreference.IsFiltering = bool.Parse(sett.Filtering); _cacheTime = DateTime.Parse(sett.CacheDate, new CultureInfo("nl-BE")); UserPreference.AccessKey = sett.AccessKey; UserPreference.FbUserId = sett.FbUserId; UserPreference.Name = sett.Name; if (sett.Associations.Equals("")) return; foreach (var asso in new List<string>(sett.Associations.Split(';'))) { var i = 0; while (i < Associtions.Count && Associtions[i].In != asso) { i++; } if (i < Associtions.Count) UserPreference.PreferredAssociations.Add(Associtions[i]); } } } }