Exemple #1
0
        /// <summary>
        /// Set một giá trị theo key và mô tả
        /// </summary>
        /// <param name="key">Khóa</param>
        /// <param name="value">Giá trị</param>
        /// <returns>Nếu đã tồn tại thì sẽ update</returns>
        private bool SetStringByKey(string key, string value)
        {
            using SystemDbContext dbContext = new SystemDbContext(new DbContextOptionsBuilder <SystemDbContext>().UseSqlServer(_connectionString).Options);
            SystemValue systemKeyValue = dbContext.SystemValues.FirstOrDefault(q => q.Key == key);

            if (systemKeyValue != null)
            {
                systemKeyValue.Value = value;
            }
            else
            {
                dbContext.SystemValues.Add(new SystemValue()
                {
                    Key = key, Value = value
                });
            }
            return(dbContext.SaveChanges() > 0);
        }
Exemple #2
0
        /// <summary>
        /// Lấy nội dung file thiết lập nếu chưa có thì tạo theo string mặc định chuyền vào
        /// </summary>
        /// <param name="settingJsonStringDefault">string mặc định</param>
        /// <param name="key">Đường dẫn đến file setting</param>
        /// <param name="description">Mô tả cho khóa dữ liệu</param>
        /// <returns>nội dung file setting</returns>
        private string GetSettingsOfCreateIfNotExist(string settingJsonStringDefault, string key, string description = "")
        {
            using SystemDbContext dbContext = new SystemDbContext(new DbContextOptionsBuilder <SystemDbContext>().UseSqlServer(_connectionString).Options);
            SystemValue systemKeyValue = dbContext.SystemValues.FirstOrDefault(q => q.Key == key);

            if (systemKeyValue != null)
            {
                return(systemKeyValue.Value);
            }

            systemKeyValue = new SystemValue()
            {
                Key = key, Value = settingJsonStringDefault, Description = description
            };
            dbContext.SystemValues.Add(systemKeyValue);
            dbContext.SaveChanges();
            return(settingJsonStringDefault);
        }