コード例 #1
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Saves the settings.
        /// </summary>
        /// <param name="config">
        ///     The config.
        /// </param>
        /// -------------------------------------------------------------------------------------------------

        public void SaveConfig(Config config = null)
        {
            if (config != null)
            {
                this.currentConfig = config;
            }

            using (SqlConfigContext configContext = new SqlConfigContext(this.nameOrConnectionString))
            {
                Dictionary <string, SiteSetting> settings = configContext.SiteSettings.Where(x => x.Name.StartsWith("Config.")).ToDictionary(x => x.Name, x => x);

                foreach (var property in Properties)
                {
                    string[]    propertyAccessors = property.Key.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                    SiteSetting setting;
                    if (!settings.ContainsKey(property.Key))
                    {
                        setting      = new SiteSetting();
                        setting.Name = property.Key;
                        configContext.SiteSettings.Add(setting);
                    }
                    else
                    {
                        setting = settings[property.Key];
                    }

                    string propertyValue = SqlConfigProvider.GetPropertyValue(this.currentConfig, propertyAccessors.Skip(1).ToList());

                    setting.Value = string.IsNullOrWhiteSpace(propertyValue) ? string.Empty : propertyValue;
                }

                configContext.SaveChanges();
            }
        }
コード例 #2
0
        /// <summary>
        /// Saves the specified configuration object.
        /// </summary>
        /// <typeparam name="T">Type of the object.</typeparam>
        /// <param name="configObject">The configuration object.</param>

        public void Save <T>(T configObject) where T : class, new()
        {
            Type   type       = typeof(T);
            string configName = type.Name;

            using (SqlConfigContext configContext = new SqlConfigContext(this.nameOrConnectionString))
            {
                IList <SiteSetting> siteSettings = configContext.SiteSettings.Where(x => x.Name.StartsWith(configName + ".")).ToList();
                if (configObject == null)
                {
                    foreach (var siteSetting in siteSettings)
                    {
                        configContext.SiteSettings.Remove(siteSetting);
                    }
                }
                else
                {
                    Dictionary <string, SiteSetting> settings = siteSettings.ToDictionary(x => x.Name, x => x);

                    foreach (var property in Build(type, configName))
                    {
                        string[]    propertyAccessors = property.Key.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                        SiteSetting setting;
                        if (!settings.ContainsKey(property.Key))
                        {
                            setting      = new SiteSetting();
                            setting.Name = property.Key;
                            configContext.SiteSettings.Add(setting);
                        }
                        else
                        {
                            setting = settings[property.Key];
                        }

                        string propertyValue = SqlConfigProvider.GetPropertyValue(configObject, propertyAccessors.Skip(1).ToList());

                        setting.Value = string.IsNullOrWhiteSpace(propertyValue) ? string.Empty : propertyValue;
                    }
                }

                configContext.SaveChanges();
            }
            this.extraConfigs.AddOrUpdate(configName, configObject, (k, obj) => configObject);
        }