Exemplo n.º 1
0
        // ---

        public async Task <string> GetDbSetting(Common.DbSetting key, string def)
        {
            string keyStr = key.ToString();

            if (keyStr.Length > DAL.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);
        }
Exemplo n.º 2
0
        public async Task <bool> SaveDbSetting(Common.DbSetting key, string value)
        {
            Settings sett = null;

            string keyStr = key.ToString();

            if (keyStr.Length > DAL.Models.Settings.MaxKeyFieldLength)
            {
                throw new Exception("DB settings key is too long");
            }
            if (value != null && value.Length > DAL.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);
        }