当前位置: 首页>>代码示例>>C#>>正文


C# IDatabase.CreateTable方法代码示例

本文整理汇总了C#中IDatabase.CreateTable方法的典型用法代码示例。如果您正苦于以下问题:C# IDatabase.CreateTable方法的具体用法?C# IDatabase.CreateTable怎么用?C# IDatabase.CreateTable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IDatabase的用法示例。


在下文中一共展示了IDatabase.CreateTable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Up

        public void Up(IDatabase db)
        {
            if (!this.IsFeatureSupported(db))
            {
                return;
            }

            db.CreateTable(Tables[0].Name, "Mig22PrimaryKey") // parent table
              .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
              .WithNotNullableColumn(Tables[0].Columns[1], DbType.String).OfSize(255);

            db.CreateTable(Tables[1].Name)
              .WithPrimaryKeyColumn(Tables[1].Columns[0], DbType.Int32).AsIdentity()
              .WithNotNullableColumn(Tables[1].Columns[1], DbType.Int32);

            db.Tables[Tables[1].Name].AddForeignKeyTo(Tables[0].Name, "Mig22ChildForeignKey")
                                     .Through(Tables[1].Columns[1], Tables[0].Columns[0])
                                     .CascadeOnDelete();

            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""{1}"") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], "Parent Row 1"));
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""{1}"") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], "Parent Row 2"));
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""{1}"") VALUES ('{2}')", Tables[1].Name, Tables[1].Columns[1], 1));
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""{1}"") VALUES ('{2}')", Tables[1].Name, Tables[1].Columns[1], 2));
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"DELETE FROM ""{0}"" WHERE ""{1}"" = 1", Tables[0].Name, Tables[0].Columns[0])); // removing the parent row should delete its child rows
        }
开发者ID:dradovic,项目名称:MigSharp,代码行数:25,代码来源:Migration22_Cascaded_Delete.cs

示例2: Up

        public void Up(IDatabase db)
        {
            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.String);

            db.Execute(GetInsertStatement((string)Tables[0].Value(0, 1)));

            // Note: the following statement does *not* the identity constraint. Doing so is very difficult. For example, see: http://stackoverflow.com/questions/702745/sql-server-how-to-drop-identity-from-a-column
            //db.Tables[TableName].Columns[ColumnNames[0]].AlterToNotNullable(DbType.Int32);

            db.Tables[Tables[0].Name].Drop(); // make sure, TRIGGERS and SEQUENCES are dropped as well

            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32)
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.String);

            // inserting another row without specifying the Id value should fail as the Identity constraint is removed
            if (db.Context.ProviderMetadata.Name != ProviderNames.SQLite) // SQLite automatically generates identity columns for PKs
            {
                db.Execute(context =>
                    {
                        // MySQL will not throw an error on insert unless strict mode is enabled
                        if (db.Context.ProviderMetadata.Name == ProviderNames.MySqlExperimental) {
                            IDbCommand command2 = context.Connection.CreateCommand();
                            command2.Transaction = context.Transaction;
                            command2.CommandText = "SET SQL_MODE = 'ANSI_QUOTES,STRICT_ALL_TABLES'";
                            command2.ExecuteNonQuery();
                        }

                        IDbCommand command = context.Connection.CreateCommand();
                        command.Transaction = context.Transaction;
                        command.CommandText = GetInsertStatement((string)Tables[0].Value(0, 1));
                        Log.Verbose(LogCategory.Sql, command.CommandText);
                        try
                        {
                            command.ExecuteNonQuery();
                            Assert.Fail("The previous query should have failed.");
                        }
                        catch (Exception x)
                        {
                            if (!x.IsDbException())
                            {
                                throw;
                            }
                        }
                    });
            }

            db.Execute(GetInsertStatement((int)Tables[0].Value(0, 0), (string)Tables[0].Value(0, 1)));

            db.Tables[Tables[0].Name].Drop(); // make sure, TRIGGERS and SEQUENCES are dropped as well

            // recreating the table with the identity constraint again might reveal undropped TRIGGERS or SEQUENCES
            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.String);

            db.Execute(GetInsertStatement((string)Tables[0].Value(0, 1)));
        }
开发者ID:mediocreguy,项目名称:MigSharp,代码行数:60,代码来源:Migration14.cs

示例3: Up

        public void Up(IDatabase db)
        {
            if (!this.IsFeatureSupported(db))
            {
                return;
            }

            db.CreateTable("Mig21b")
              .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
              .WithNotNullableColumn("Content", DbType.String).OfSize(255);

            db.CreateTable("Mig21a")
              .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
              .WithRowVersionColumn("Version")
              .WithNotNullableColumn("Content", DbType.String).OfSize(255);
            db.Tables["Mig21b"].AddRowVersionColumn("Version");

            db.Execute(context =>
                {
                    IDbCommand command = context.CreateCommand();

                    InsertAndUpdateRow(command, "Mig21a", context);
                    InsertAndUpdateRow(command, "Mig21b", context);
                });
        }
开发者ID:dradovic,项目名称:MigSharp,代码行数:25,代码来源:Migration21_RowVersion.cs

示例4: Up

        public void Up(IDatabase db)
        {
            if (!this.IsFeatureSupported(db))
            {
                return;
            }

            db.CreateTable("Mig23a")
              .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
              .WithNotNullableColumn("Data", DbType.String).OfSize(255);
            db.CreateTable("Mig23b")
              .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
              .WithNotNullableColumn("Data", DbType.String).OfSize(255);

            // INSERT using literal SQL (something which could be considered bad practice is most cases)
            string unicodeLiteralPrefix = db.Context.ProviderMetadata.PrefixUnicodeLiterals ? "N" : string.Empty; // see: http://stackoverflow.com/questions/31270356/is-nsome-string-here-ansi-sql
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""Mig23a"" (""Data"") VALUES ({0}'Irgendöppis')", unicodeLiteralPrefix));
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""Mig23a"" (""Data"") VALUES ({0}'Unicodović')", unicodeLiteralPrefix));

            // INSERT using parameter injection
            db.Execute(ctx =>
                {
                    InsertUsingParameters(ctx, "Irgendöppis");
                    InsertUsingParameters(ctx, "Unicodović");
                });
        }
开发者ID:dradovic,项目名称:MigSharp,代码行数:26,代码来源:Migration23_Unicode.cs

示例5: Up

        public void Up(IDatabase db)
        {
            if (!this.IsFeatureSupported(db))
            {
                return;
            }

            db.CreateTable(Tables[0].Name)
              .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
              .WithNotNullableColumn(Tables[0].Columns[1], DbType.String).OfSize(255);
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], Tables[1].Value(0, 1)));
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], Tables[1].Value(1, 1)));

            db.Tables[Tables[0].Name].PrimaryKey().Rename("PK_" + NewTableName);
            db.Tables[Tables[0].Name].Rename(NewTableName);
            // insert another row into the renamed table to verify that the Identity is still working
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", NewTableName, Tables[1].Columns[1], Tables[1].Value(2, 1)));

            // create a new table that has the same name as the previously renamed table and also has an identity column to check
            // if associated db objects that are managed by the provider itself were renamed along with the table
            db.CreateTable(Tables[0].Name)
              .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
              .WithNotNullableColumn(Tables[0].Columns[1], DbType.String).OfSize(255);
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], Tables[0].Value(0, 1)));
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], Tables[0].Value(1, 1)));
        }
开发者ID:dradovic,项目名称:MigSharp,代码行数:26,代码来源:Migration17_Clearing_PKs_Journey.cs

示例6: Up

        public void Up(IDatabase db)
        {
            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.String).OfSize(255);
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], Tables[1].Value(0, 1)));
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], Tables[1].Value(1, 1)));

            if (db.Context.ProviderMetadata.Name != ProviderNames.SqlServerCe4 &&
                db.Context.ProviderMetadata.Name != ProviderNames.SqlServerCe35 &&
                db.Context.ProviderMetadata.Name != ProviderNames.SQLite &&
                !db.Context.ProviderMetadata.Name.Contains("Teradata"))
            {
                db.Tables[Tables[0].Name].PrimaryKey().Rename("PK_" + NewTableName);
            }
            else if (db.Context.ProviderMetadata.Name == ProviderNames.SqlServerCe4 || db.Context.ProviderMetadata.Name == ProviderNames.SqlServerCe35)
            {
                // this code is actually not required for the test but we still execute it because it is what the recommendation
                // of the SqlServerCe4 and SqlServerCe35 is in the NotSupportedException for the primary key renaming
                db.Tables[Tables[0].Name].PrimaryKey().Drop();
                db.Tables[Tables[0].Name].AddPrimaryKey("PK_" + NewTableName)
                    .OnColumn(Tables[0].Columns[0]);
            }
            db.Tables[Tables[0].Name].Rename(NewTableName);
            // insert another row into the renamed table to verify that the Identity is still working
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", NewTableName, Tables[1].Columns[1], Tables[1].Value(2, 1)));

            // create a new table that has the same name as the previously renamed table and also has an identity column to check
            // if associated db objects that are managed by the provider itself were renamed along with the table
            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.String).OfSize(255);
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], Tables[0].Value(0, 1)));
            db.Execute(string.Format(CultureInfo.InvariantCulture, "INSERT INTO \"{0}\" (\"{1}\") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], Tables[0].Value(1, 1)));
        }
开发者ID:richardprior,项目名称:MigSharp,代码行数:35,代码来源:Migration17.cs

示例7: Up

        public void Up(IDatabase db)
        {
            _rowVersionColumnIsSupported = db.Context.ProviderMetadata.Name.StartsWith("SqlServer", StringComparison.Ordinal);

            db.CreateTable("Mig21b")
                  .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
                  .WithNotNullableColumn("Content", DbType.String).OfSize(255);

            if (_rowVersionColumnIsSupported)
            {
                db.CreateTable("Mig21a")
                  .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
                  .WithRowVersionColumn("Version")
                  .WithNotNullableColumn("Content", DbType.String).OfSize(255);
                db.Tables["Mig21b"].AddRowVersionColumn("Version");
            }
            else
            {
                db.CreateTable("Mig21a")
                  .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
                  .WithNotNullableColumn("Version", DbType.Int64)
                  .WithNotNullableColumn("Content", DbType.String).OfSize(255);
                db.Tables["Mig21b"].AddNotNullableColumn("Version", DbType.Int64).HavingDefault(1L);
            }

            db.Execute(context =>
                {
                    IDbCommand command = context.Connection.CreateCommand();
                    command.Transaction = context.Transaction;

                    InsertAndUpdateRow(command, "Mig21a", context);
                    InsertAndUpdateRow(command, "Mig21b", context);
                });
        }
开发者ID:mediocreguy,项目名称:MigSharp,代码行数:34,代码来源:Migration21.cs

示例8: Up

        public void Up(IDatabase db)
        {
            db.CreateTable(Tables[0].Name, "first")
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.Int32); // FK to 'other'

            const string otherId = "Id";
            const string otherName = "Name";
            db.CreateTable(Other)
                .WithPrimaryKeyColumn(otherId, DbType.Int32).AsIdentity()
                .WithNotNullableColumn(otherName, DbType.String).OfSize(255);

            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""{1}"") VALUES ('{2}')", Other, otherName, "Not Referenced"));
            db.Execute(GetDeleteStatementForOther()); // removing the row from Other should not be a problem since it is not referenced
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""{1}"") VALUES ('{2}')", Other, otherName, "Referenced"));

            // testing to see that if we drop an index rename the table create a copy of the table with the same old fk it should work (TD issue in security)
            db.Tables[Tables[0].Name].AddForeignKeyTo(Other, "test")
                .Through(Tables[0].Columns[1], otherId);
            db.Tables[Tables[0].Name].ForeignKeys["test"].Drop();
            db.Tables[Tables[0].Name].Rename(TmpPrefix + Tables[0].Name);

            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.Int32); // FK to 'other

            db.Tables[Tables[0].Name].AddForeignKeyTo(Other, "test")
                .Through(Tables[0].Columns[1], otherId);

            // insert a row that references a row from Other
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""{1}"") VALUES ('{2}')", Tables[0].Name, Tables[0].Columns[1], Tables[0].Value(0, 1)));

            // removing the row from Other should fail as it is referenced by the foreign key
            if (db.Context.ProviderMetadata.Name != ProviderNames.SQLite) // Mig# does not support SQLite foreign keys (see comments in SQLiteProvider.AddForeignKey)
            {
                db.Execute(context =>
                    {
                        IDbCommand command = context.Connection.CreateCommand();
                        command.Transaction = context.Transaction;
                        command.CommandText = GetDeleteStatementForOther();
                        Log.Verbose(LogCategory.Sql, command.CommandText);
                        try
                        {
                            command.ExecuteNonQuery();
                            Assert.Fail("The previous query should have failed.");
                        }
                        catch (Exception x)
                        {
                            // a DbException is expected (for the case of a SqlServer35 the SqlCeException is not derived from DbException)
                            if (!(x is DbException) && x.GetType().Name != "SqlCeException")
                            {
                                throw;
                            }
                        }
                    });
            }
        }
开发者ID:richardprior,项目名称:MigSharp,代码行数:57,代码来源:Migration13.cs

示例9: Up

        public void Up(IDatabase db)
        {
            if (db.Context.ProviderMetadata.Name != ProviderNames.SQLite) // SQLite does not support altering of columns
            {
                db.CreateTable(Tables[0].Name)
                    .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                    .WithNotNullableColumn(Tables[0].Columns[1], DbType.AnsiString).OfSize(2000)
                    .WithNotNullableColumn(Tables[0].Columns[2], DbType.String).OfSize(2000)
                    .WithNotNullableColumn(Tables[0].Columns[3], DbType.AnsiString).OfSize(2000);
            }
            else
            {
                db.CreateTable(Tables[0].Name)
                    .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                    .WithNotNullableColumn(Tables[0].Columns[1], DbType.String).OfSize(2000)
                    .WithNullableColumn(Tables[0].Columns[2], DbType.String).OfSize(2000)
                    .WithNullableColumn(Tables[0].Columns[3], DbType.String).OfSize(2000);
            }

            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""First"", ""Second"", ""Third"") VALUES ('{1}', '{2}', '{3}')", Tables[0].Name, Tables[0].Value(0, 1), Tables[0].Value(0, 2), Tables[0].Value(0, 3)));
            if (db.Context.ProviderMetadata.Name != ProviderNames.SQLite)
            {
                db.Tables[Tables[0].Name].Columns[Tables[0].Columns[1]].AlterToNotNullable(DbType.String).OfSize(2000); // changing the type from AnsiString to String should be possible without further problems (required by migration 7 of the Security Component)
                db.Tables[Tables[0].Name].Columns[Tables[0].Columns[2]].AlterToNullable(DbType.String).OfSize(2000); // changing the nullability but keeping the current datatype
                db.Tables[Tables[0].Name].Columns[Tables[0].Columns[3]].AlterToNullable(DbType.String).OfSize(2000); // changing the nullability *and* the datatype at the same time
            }
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""First"") VALUES ('{1}')", Tables[0].Name, Tables[0].Value(1, 1))); // try to execute without second description and third description as they should allow null now
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"UPDATE ""{0}"" SET ""{1}""='{2}' WHERE ""{1}"" IS NULL", Tables[0].Name, Tables[0].Columns[3], Tables[0].Value(1, 3)));
            if (db.Context.ProviderMetadata.Name != ProviderNames.SQLite)
            {
                db.Tables[Tables[0].Name].Columns[Tables[0].Columns[3]].AlterToNotNullable(DbType.String).OfSize(2000).HavingDefault(Default3);
            }
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""First"") VALUES ('{1}')", Tables[0].Name, Tables[0].Value(2, 1))); // try again to execute without third description as it should have a default now
            if (db.Context.ProviderMetadata.Name == ProviderNames.SQLite)
            {
                // simulate the default
                db.Execute(string.Format(CultureInfo.InvariantCulture, @"UPDATE ""{0}"" SET ""{1}""='{2}' WHERE ""{1}"" IS NULL", Tables[0].Name, Tables[0].Columns[3], ((string)Tables[0].Value(2, 3)).Replace("'", "''")));
            }
            if (db.Context.ProviderMetadata.Name != ProviderNames.SQLite)
            {
                db.Tables[Tables[0].Name].Columns[Tables[0].Columns[3]].AlterToNotNullable(DbType.String).OfSize(2000).HavingDefault(SecondDefault3); // change the default
            }
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""First"") VALUES ('{1}')", Tables[0].Name, Tables[0].Value(3, 1))); // try again to execute without third description as it should have another default now
            if (db.Context.ProviderMetadata.Name == ProviderNames.SQLite)
            {
                // simulate the default
                db.Execute(string.Format(CultureInfo.InvariantCulture, @"UPDATE ""{0}"" SET ""{1}""='{2}' WHERE ""{1}"" IS NULL", Tables[0].Name, Tables[0].Columns[3], ((string)Tables[0].Value(3, 3)).Replace("'", "''")));
            }
            if (db.Context.ProviderMetadata.Name != ProviderNames.SQLite)
            {
                db.Tables[Tables[0].Name].Columns[Tables[0].Columns[3]].AlterToNullable(DbType.String).OfSize(2000); // remove the Default3 default value again
            }
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" (""First"") VALUES ('{1}')", Tables[0].Name, Tables[0].Value(4, 1))); // try again to execute without third description, and this time it should be NULL
        }
开发者ID:mediocreguy,项目名称:MigSharp,代码行数:54,代码来源:Migration5.cs

示例10: Up

        public void Up(IDatabase db)
        {
            if (!db.Context.ProviderMetadata.Name.Contains("Teradata") && db.Context.ProviderMetadata.Name != ProviderNames.SQLite)
            {
                db.CreateTable(Tables[0].Name)
                    .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32)
                    .WithNotNullableColumn(Tables[0].Columns[1], DbType.Int32)
                    .WithNotNullableColumn(Tables[0].Columns[2], DbType.Int32);
            }
            else
            {
                // Teradata and SQLite do not allow adding/dropping primary keys
                db.CreateTable(Tables[0].Name)
                    .WithNotNullableColumn(Tables[0].Columns[0], DbType.Int32)
                    .WithNotNullableColumn(Tables[0].Columns[1], DbType.Int32)
                    .WithNotNullableColumn(Tables[0].Columns[2], DbType.Int32);
            }

            // insert first record
            db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" VALUES({1}, {2}, {3})",
                Tables[0].Name,
                Tables[0].Value(0, 0),
                Tables[0].Value(0, 1),
                Tables[0].Value(0, 2)));

            // add and drop primary keys
            if (!db.Context.ProviderMetadata.Name.Contains("Teradata") && db.Context.ProviderMetadata.Name != ProviderNames.SQLite)
            {
                db.Tables[Tables[0].Name].PrimaryKey().Drop();

                db.Tables[Tables[0].Name].AddPrimaryKey("Test Mig15")
                    .OnColumn(Tables[0].Columns[1]);
                db.Tables[Tables[0].Name].PrimaryKey("Test Mig15").Drop();

                db.Tables[Tables[0].Name].AddPrimaryKey()
                    .OnColumn(Tables[0].Columns[2]);
                db.Tables[Tables[0].Name].PrimaryKey().Drop();
            }

            // there should be no primary key now, so adding the same values should be ok
            if (db.Context.ProviderMetadata.Name != ProviderNames.TeradataOdbc) // the Teradata *ODBC* driver auto-creates *unique* primary indexes which would lead to a "Duplicate row error"
            {
                db.Execute(string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" VALUES({1}, {2}, {3})",
                    Tables[0].Name,
                    Tables[0].Value(0, 0),
                    Tables[0].Value(0, 1),
                    Tables[0].Value(0, 2)));
                _onlyExpectOneRecord = false;
            }
            else
            {
                _onlyExpectOneRecord = true;
            }
        }
开发者ID:mediocreguy,项目名称:MigSharp,代码行数:54,代码来源:Migration15.cs

示例11: Up

        public void Up(IDatabase db)
        {
            db.CreateTable("DomainEvent")
                .WithPrimaryKeyColumn("Id", DbType.Guid)
                .WithNotNullableColumn("UserId", DbType.Guid)
                .WithNotNullableColumn("DateTime", DbType.DateTime)
                .WithNotNullableColumn("Event", DbType.String);

            db.CreateTable("User")
                .WithPrimaryKeyColumn("Id", DbType.Guid)
                .WithNotNullableColumn("Name", DbType.StringFixedLength).OfSize(255);
        }
开发者ID:caiokf,项目名称:smart-track,代码行数:12,代码来源:001_create_initial_tables.cs

示例12: Up

        public void Up(IDatabase db)
        {
            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.String);

            db.Execute(GetInsertStatement((string)Tables[0].Value(0, 1)));

            // Note: the following statement does *not* the identity constraint. Doing so is very difficult. For example, see: http://stackoverflow.com/questions/702745/sql-server-how-to-drop-identity-from-a-column
            //db.Tables[TableName].Columns[ColumnNames[0]].AlterToNotNullable(DbType.Int32);

            db.Tables[Tables[0].Name].Drop(); // make sure, TRIGGERS and SEQUENCES are dropped as well

            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32)
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.String);

            // inserting another row without specifying the Id value should fail as the Identity constraint is removed
            if (db.Context.ProviderMetadata.Name != ProviderNames.SQLite) // SQLite automatically generates identity columns for PKs
            {
                db.Execute(context =>
                    {
                        IDbCommand command = context.Connection.CreateCommand();
                        command.Transaction = context.Transaction;
                        command.CommandText = GetInsertStatement((string)Tables[0].Value(0, 1));
                        Log.Verbose(LogCategory.Sql, command.CommandText);
                        try
                        {
                            command.ExecuteNonQuery();
                            Assert.Fail("The previous query should have failed.");
                        }
                        catch (Exception ex)
                        {
                            // this is expected
                            if (db.Context.ProviderMetadata.Name != ProviderNames.SqlServerCe35 && !(ex is DbException))
                                throw;
                            // for the case of a SqlServer35 the SqlCeException is not derived from DbException
                        }
                    });
            }

            db.Execute(GetInsertStatement((int)Tables[0].Value(0, 0), (string)Tables[0].Value(0, 1)));

            db.Tables[Tables[0].Name].Drop(); // make sure, TRIGGERS and SEQUENCES are dropped as well

            // recreating the table with the identity constraint again might reveal undropped TRIGGERS or SEQUENCES
            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.String);

            db.Execute(GetInsertStatement((string)Tables[0].Value(0, 1)));
        }
开发者ID:balistof,项目名称:MigSharp,代码行数:52,代码来源:Migration14.cs

示例13: Up

        public void Up(IDatabase db)
        {
            db.CreateTable(Tables[0].Name)
                .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
                .WithNotNullableColumn(Tables[0].Columns[1], DbType.Decimal).OfSize(3)
                .WithNotNullableColumn(Tables[0].Columns[2], DbType.Decimal).OfSize(5, 2);

            if (db.Context.ProviderMetadata.Name != ProviderNames.SQLite) // SQLite uses adaptiv algorithms for their data types: http://www.sqlite.org/datatype3.html
            {
                db.Execute(GetInsertStatement((decimal)Tables[0].Value(0, 2) + 0.003m)); // the extra precision should be cut off silently
                db.Execute(context =>
                    {
                        IDbCommand command = context.Connection.CreateCommand();
                        command.Transaction = context.Transaction;
                        command.CommandText = GetInsertStatement(1000m);
                        Log.Verbose(LogCategory.Sql, command.CommandText);
                        try
                        {
                            command.ExecuteNonQuery();
                            Assert.Fail("The previous query should have failed.");
                        }
                        catch (DbException)
                        {
                            // this is expected
                        }
                    });
            }
            else
            {
                db.Execute(GetInsertStatement((decimal)Tables[0].Value(0, 2)));
            }
        }
开发者ID:nachojammers,项目名称:MigSharp,代码行数:32,代码来源:Migration11.cs

示例14: Up

 public void Up(IDatabase db)
 {
     db.CreateTable(_tableName)
         .WithPrimaryKeyColumn(TimestampColumnName, DbType.Int64)
         .WithPrimaryKeyColumn(ModuleColumnName, DbType.String).OfSize(MigrationExportAttribute.MaximumModuleNameLength)
         .WithNullableColumn(TagColumnName, DbType.String).OfSize(2000);
 }
开发者ID:mediocreguy,项目名称:MigSharp,代码行数:7,代码来源:BootstrapMigration.cs

示例15: Up

 public void Up(IDatabase db)
 {
     db.CreateTable(FjosTable)
         .WithPrimaryKeyColumn("Id", DbType.Int64).AsIdentity()
         .WithNotNullableColumn("BondegardId", DbType.Int64);
     db.Tables[FjosTable].AddForeignKeyTo(BondegardTable).Through("BondegardId", "Id");
 }
开发者ID:pedershk,项目名称:dotnetprograms,代码行数:7,代码来源:V005_AddFjos_5.cs


注:本文中的IDatabase.CreateTable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。