Exemplo n.º 1
0
        void IProviderExtend.CreateForeignKeys(MetaTable metaTable)
        {
            //  var metaTable = services.Model.GetTable(metaTable);
            var commands = MySqlBuilder.GetCreateForeignKeyCommands(metaTable);

            foreach (var command in commands)
            {
                services.Context.ExecuteCommand(command);
            }
        }
Exemplo n.º 2
0
        public void CreateDatabase()
        {
            if (DatabaseExists())
            {
                throw SqlClient.Error.CreateDatabaseFailedBecauseSqlCEDatabaseAlreadyExists(dbName);
            }

            //创建数据库文件
            var connection = new MySqlConnection(GetConnectionString(string.Empty));

            connection.Open();

            var transaction = connection.BeginTransaction();

            Execute(connection, transaction, MySqlBuilder.GetCreateDatabaseCommand(dbName, null, null));
            try
            {
                MetaModel model = services.Model;
                foreach (MetaTable table in model.GetTables())
                {
                    string createTableCommand = MySqlBuilder.GetCreateTableCommand(table);

                    if (!string.IsNullOrEmpty(createTableCommand))
                    {
                        Execute(connection, transaction, createTableCommand);
                    }
                }

                foreach (MetaTable table in model.GetTables())
                {
                    foreach (string commandText in MySqlBuilder.GetCreateForeignKeyCommands(table))
                    {
                        if (!string.IsNullOrEmpty(commandText))
                        {
                            Execute(connection, transaction, commandText);
                        }
                    }
                }

                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
            finally
            {
                connection.Close();
            }
        }