private static void InsertConnectionString(ApplyOptions options, SettingsProviderDatabase database, SettingsProvider.Section section, Action action) {
            var name = new SqlConnectionName() {
                Name = action.Name
            };

            SqlConnectionString connection = null;
            if (!options.UseExistingConnectionStrings) {
                connection = database.SqlConnectionStrings.FirstOrDefault(c => c.ConnectionString == action.Value);
            }
            if (connection == null) {
                connection = new SqlConnectionString() {
                    ConnectionString = action.Value
                };
                database.AddToSqlConnectionStrings(connection);
            }

            database.AddToSqlConnectionNames(name);

            name.Section = section;
            name.SqlConnectionString = connection;
        }
 public static SnapShot GetFor(string sectionName) {
     SnapShot ret = new SnapShot();
     ret.Section = sectionName;
     
     using (var database = new SettingsProviderDatabase()) {
         var section = database.Sections.FirstOrDefault(s => s.Name == sectionName);
         if (section != null)
             ret.Assembly = section.AssemblyName;
         ret.Settings = database.GetApplcationSettingsFor(sectionName);
         ret.ConnectionStrings = database.GetNamedConnectionsFor(sectionName);
     }
     return ret;
 }
        public void Apply(ApplyOptions options) {
            if (IsEmpty)
                return;

            using (var database = new SettingsProviderDatabase()) {
                var section = database.Sections.FirstOrDefault(s => s.Name == Section);
                if (section == null) {
                    section = new Section() { Name = Section, AssemblyName = Assembly };
                    database.AddToSections(section);
                }

                foreach (Action action in Actions) {
                    switch (action.Type) {
                        case Action.ActionType.Insert:
                            if (action.IsConnectionString) 
                                InsertConnectionString(options, database, section, action);
                            else 
                                InsertApplcationSetting(section, action);
                            break;
                        case Action.ActionType.Delete:
                            if (action.IsConnectionString) {
                                var names = from connectionName in database.SqlConnectionNames.Include("Section")
                                            where connectionName.Section.Id == section.Id && connectionName.Name == action.Name
                                            select connectionName;
                                foreach (var name in names) database.DeleteObject(name);
                            } else {
                                var settings = from setting in database.ApplicationSettings
                                               where setting.SectionId == section.Id && setting.Name == action.Name
                                               select setting;
                                foreach (var name in settings) database.DeleteObject(name);
                            }
                            break;
                        case Action.ActionType.Update:
                            if (action.IsConnectionString) {
                                SqlConnectionString connection = database.SqlConnectionNames
                                    .Include("Section")
                                    .Include("SqlConnectionString")
                                    .Where(sec => sec.Section.Id == section.Id)
                                    .Select(sec => sec.SqlConnectionString)
                                    .FirstOrDefault();
                                if(connection != null)
                                    connection.ConnectionString = action.Value;
                            } else {
                                ApplicationSetting setting = database.ApplicationSettings
                                    .Include("Section")
                                    .Where(s => s.Section.Id == section.Id && s.Name == action.Name)
                                    .FirstOrDefault();
                                if (setting != null)
                                    setting.Value = action.Value;
                            }
                            break;
                    }
                }
                database.SaveChanges();
            }
        }