示例#1
0
        private static void createScope(SqlCeConnection connection)
        {
            var scopeDesc = new DbSyncScopeDescription("SyncScope");

            // Definition for Customer.
            DbSyncTableDescription customerDescription =
                SqlCeSyncDescriptionBuilder.GetDescriptionForTable("Company", connection);

            scopeDesc.Tables.Add(customerDescription);

            customerDescription =
                SqlCeSyncDescriptionBuilder.GetDescriptionForTable("Contact", connection);
            scopeDesc.Tables.Add(customerDescription);

            // Create a provisioning object for "SyncScope". We specify that
            // base tables should not be created (They already exist in SyncSamplesDb_SqlPeer1),
            // and that all synchronization-related objects should be created in a
            // database schema named "Sync". If you specify a schema, it must already exist
            // in the database.
            var serverConfig = new SqlCeSyncScopeProvisioning(scopeDesc);

            serverConfig.SetCreateTableDefault(DbSyncCreationOption.Create);

            // Configure the scope and change-tracking infrastructure.
            serverConfig.Apply(connection);
            connection.Close();
        }
示例#2
0
        /// <summary>
        /// Добавляет таблицы в область синхронизации.
        /// </summary>
        /// <param name="tableNames"></param>
        /// <param name="scope"></param>
        /// <param name="connection"></param>
        /// <returns>Таблицы, которые не были добавлены.</returns>
        private static IList <string> AddTablesToScopeDescr(IEnumerable <string> tableNames, DbSyncScopeDescription scope, DbConnection connection)
        {
            var failed = new List <string>();

            Poster.PostMessage("Adding tables to scope '{0}' in '{1}'...", scope.ScopeName, connection.ConnectionString);

            foreach (var name in tableNames)
            {
                try
                {
                    DbSyncTableDescription desc;

                    // не создаем описание для таблицы повторно, после провизионирования другой области с этой таблицей
                    var nameWithSchema = connection is SqlCeConnection ? name : name.GetSchemaForTable() + '.' + name;
                    var nameWithConn   = connection.ConnectionString + nameWithSchema;

                    if (!map.TryGetValue(nameWithConn, out desc))
                    {
                        if (connection is SqlCeConnection)
                        {
                            desc = SqlCeSyncDescriptionBuilder.GetDescriptionForTable(nameWithSchema, connection as SqlCeConnection);
                        }
                        else if (connection is SqlConnection)
                        {
                            desc = SqlSyncDescriptionBuilder.GetDescriptionForTable(nameWithSchema, connection as SqlConnection);
                        }

                        map[nameWithConn] = desc;
                    }
                    else
                    {
                        Poster.PostMessage("Reuse created Description For Table '{0}'", name);
                    }

                    desc.GlobalName = name;
                    scope.Tables.Add(desc);
                    Poster.PostMessage("Table '{0}' added, columns: {1}", name, string.Join(", ", desc.Columns.Select(x => x.UnquotedName)));
                }
                catch (Exception ex)
                {
                    Poster.PostMessage(ex);
                    failed.Add(name);
                }
            }
            return(failed);
        }