// --- public async Task <string> GetDbSetting(DbSetting key, string def) { string keyStr = key.ToString(); if (keyStr.Length > Models.Settings.MaxKeyFieldLength) { throw new Exception("DB settings key is too long"); } try { var sett = await( from s in this.Settings where s.Key == keyStr select s ) .AsNoTracking() .FirstOrDefaultAsync(); if (sett != null) { return(sett.Value); } } catch { } return(def); }
public async Task <bool> SaveDbSetting(DbSetting key, string value) { Settings sett = null; string keyStr = key.ToString(); if (keyStr.Length > Models.Settings.MaxKeyFieldLength) { throw new Exception("DB settings key is too long"); } if (value != null && value.Length > Models.Settings.MaxValueFieldLength) { throw new Exception("DB settings value is too long"); } try { sett = await( from s in this.Settings where s.Key == keyStr select s ) .FirstOrDefaultAsync(); if (sett != null) { sett.Value = value; } else { sett = new Settings() { Key = keyStr, Value = value, }; this.Settings.Add(sett); } await SaveChangesAsync(); return(true); } catch { } finally { if (sett != null) { this.Entry(sett).State = EntityState.Detached; } } return(false); }