private static SelectedConfigSections FillDefaults(ArgsParser parser, SyncConfigurationSection syncConfig) { SelectedConfigSections sections = new SelectedConfigSections(); if (string.IsNullOrEmpty(parser.ScopeName) && syncConfig.SyncScopes.Count == 1) { sections.SelectedSyncScope = syncConfig.SyncScopes.Cast<SyncScopeConfigElement>().First(); parser.ScopeName = sections.SelectedSyncScope.Name; } else { sections.SelectedSyncScope = syncConfig.SyncScopes.Cast<SyncScopeConfigElement>().Single((e) => e.Name.Equals(parser.ScopeName, StringComparison.InvariantCultureIgnoreCase)); } if (string.IsNullOrEmpty(parser.TargetDatabaseName) && syncConfig.Databases.Count == 1) { sections.SelectedTargetDatabase = syncConfig.Databases.Cast<TargetDatabaseConfigElement>().First(); parser.TargetDatabaseName = sections.SelectedTargetDatabase.Name; } else { sections.SelectedTargetDatabase = syncConfig.Databases.Cast<TargetDatabaseConfigElement>().Single((e) => e.Name.Equals(parser.TargetDatabaseName, StringComparison.InvariantCultureIgnoreCase)); } return sections; }
private static DbSyncScopeDescription GetDbSyncScopeDescription(SelectedConfigSections selectedConfig) { DbSyncScopeDescription desc = new DbSyncScopeDescription(selectedConfig.SelectedSyncScope.Name); using (SqlConnection conn = new SqlConnection(selectedConfig.SelectedTargetDatabase.GetConnectionString())) { conn.Open(); foreach (SyncTableConfigElement table in selectedConfig.SelectedSyncScope.SyncTables) { DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(table.Name, conn); // Ensure all specified columns do belong to the table on the server. foreach (SyncColumnConfigElement colElem in table.SyncColumns) { if (tableDesc.Columns.Where((e) => e.UnquotedName.Equals(colElem.Name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault() == null) { throw new InvalidOperationException(string.Format("Table '{0}' does not have a column '{1}' defined in the target database. Please check your SyncColumn definitions", table.Name, colElem.Name)); } } List<DbSyncColumnDescription> columnsToRemove = new List<DbSyncColumnDescription>(); // Mark timestamp columns for removal columnsToRemove.AddRange(tableDesc.Columns.Where( e => ((SqlDbType)Enum.Parse(typeof(SqlDbType), e.Type, true)) == SqlDbType.Timestamp) ); if (!table.IncludeAllColumns || table.SyncColumns.Count > 0) { //Users wants a subset of columns. Remove the ones they are not interested in foreach (DbSyncColumnDescription columnDesc in tableDesc.Columns) { SyncColumnConfigElement configElement = table.SyncColumns.Cast<SyncColumnConfigElement>().FirstOrDefault((e) => e.Name.Equals(columnDesc.UnquotedName, StringComparison.InvariantCultureIgnoreCase)); if (configElement == null) { // Found a column that was not specified by the user. Remove it columnsToRemove.Add(columnDesc); } else { columnDesc.IsNullable = configElement.IsNullable; columnDesc.IsPrimaryKey = configElement.IsPrimaryKey; } } } // Remove columns marked for removal columnsToRemove.ForEach((e) => tableDesc.Columns.Remove(e)); // Check to see that columns count is greater than 0 if (tableDesc.Columns.Count == 0) { throw new InvalidOperationException( string.Format("SyncTable '{0}' has zero SyncColumns configured for sync. Either set IncludeAllColumns to true or specify atleast one SyncColumn.", table.Name)); } // Fill in global name if (!string.IsNullOrEmpty(table.GlobalName)) { tableDesc.GlobalName = table.GlobalName; } desc.Tables.Add(tableDesc); } } return desc; }