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


C# Database.AlterTable方法代码示例

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


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

示例1: AddPrimaryKeyConstraintTest

        public void AddPrimaryKeyConstraintTest()
        {
            Database db = new Database(new MockMigrationContext(Target));
            db.AlterTable("MyTable").AddPrimaryKeyConstraint("MyPK", new string[] { "MyColumn1", "MyColumn2" });
            var pk = db.MigrationSteps.FirstOrDefault(ms => ms is PrimaryKeyConstraint) as PrimaryKeyConstraint;

            string[] sql = Target.AddPrimaryKeyConstraint(pk).ToArray();
            Assert.AreEqual(1, sql.Length);
            Assert.AreEqual("ALTER TABLE [MyTable] ADD CONSTRAINT [MyPK] PRIMARY KEY ([MyColumn1], [MyColumn2]);", sql[0]);
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:10,代码来源:SqlServerProviderTest.cs

示例2: AddForeignKeyConstraintTest

        public void AddForeignKeyConstraintTest()
        {
            Database db = new Database(new MockMigrationContext(Target));
            db.AlterTable("MyTable").AddForeignKeyConstraint("MyFK", "MyColumn", "MyRelatedTable", "MyRelatedColumn");
            var fk = db.MigrationSteps.FirstOrDefault(ms => ms is ForeignKeyConstraint) as ForeignKeyConstraint;

            string[] sql = Target.AddForeignKeyConstraint(fk).ToArray();
            Assert.AreEqual(1, sql.Length);
            Assert.AreEqual("ALTER TABLE [MyTable] ADD CONSTRAINT [MyFK] FOREIGN KEY ([MyColumn]) REFERENCES [MyRelatedTable] ([MyRelatedColumn]);", sql[0]);
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:10,代码来源:SqlServerProviderTest.cs

示例3: AddItem

        /// <summary>
        /// Stores a new migration history record in the database.
        /// </summary>
        /// <param name="context">The migration context.</param>
        /// <param name="item">The item.</param>
        public virtual void AddItem(MigrationContext context, MigrationHistoryItem item)
        {
            EnsureMigrationHistorySchemaExists(context);

            Database model = new Database(null);
            Table t = model.AlterTable(TableName);

            var row = new Dictionary<string, object>();
            row.Add(DateColumnName, item.Date);
            row.Add(VersionColumnName, item.Version);
            row.Add(DirectionColumnName, (int)item.Direction);
            t.Insert(row);

            foreach (string sql in context.SqlProvider.GenerateSqlCommands(model))
            {
                context.SqlProcessor.ProcessEngineStatement(context, sql);
            }
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:23,代码来源:MigrationHistoryRepository.cs

示例4: AddUniqueConstraintTest

        public void AddUniqueConstraintTest()
        {
            Database db = new Database(new MockMigrationContext(Target));
            db.AlterTable("MyTable").AddUniqueConstraint("MyPK", new string[] { "MyColumn1" });
            var unique = db.MigrationSteps.FirstOrDefault(ms => ms is UniqueConstraint) as UniqueConstraint;

            string[] sql = Target.AddUniqueConstraint(unique).ToArray();
            Assert.AreEqual(1, sql.Length);
            Assert.AreEqual("ALTER TABLE [MyTable] ADD CONSTRAINT [MyPK] UNIQUE ([MyColumn1]);", sql[0]);
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:10,代码来源:SqlServerProviderTest.cs

示例5: UpdateTest

        public void UpdateTest()
        {
            Database db = new Database(new MockMigrationContext(Target));
            db.AlterTable("MyTable").Update(
                new
                {
                    Column1 = "NewValue",
                    Column2 = 3
                },
                new
                {
                    Column2 = 4,
                    Column3 = true
                }
            );

            string[] sql = Target.Update(db.MigrationSteps.ToArray()[1] as Update).ToArray();
            Assert.AreEqual(1, sql.Length);
            Assert.AreEqual("UPDATE [MyTable] SET [Column1] = 'NewValue', [Column2] = 3 WHERE [Column2] = 4 AND [Column3] = 1;", sql[0]);
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:20,代码来源:SqlServerProviderTest.cs

示例6: InsertTest

        public void InsertTest()
        {
            Database db = new Database(new MockMigrationContext(Target));
            db.AlterTable("MyTable").Insert(
                new
                {
                    MyColumn1 = new DateTime(1970, 2, 24),
                    MyColumn2 = "Test",
                    MyColumn3 = 23
                }
            );

            string[] sql = Target.Insert(db.MigrationSteps.ToArray()[1] as Insert).ToArray();
            Assert.AreEqual(1, sql.Length);
            Assert.AreEqual("INSERT INTO [MyTable] ([MyColumn1], [MyColumn2], [MyColumn3]) VALUES('1970-02-24', 'Test', 23);", sql[0]);
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:16,代码来源:SqlServerProviderTest.cs

示例7: DatabaseModel5Test

        public void DatabaseModel5Test()
        {
            Database db = new Database(new MockMigrationContext(Target));
            var t = db.AlterTable("Supersessions");
            t.DropForeignKeyConstraint("FK_Supersession_Client");
            t.DropColumn("ClientID");

            string[] sql = Target.GenerateSqlCommands(db).ToArray();
            Assert.AreEqual(2, sql.Length);
            Assert.AreEqual("ALTER TABLE [Supersessions] DROP CONSTRAINT [FK_Supersession_Client];", sql[0]);
            Assert.AreEqual("ALTER TABLE [Supersessions] DROP COLUMN [ClientID];", sql[1]);
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:12,代码来源:SqlServerProviderTest.cs

示例8: DatabaseModel4Test

        public void DatabaseModel4Test()
        {
            Database db = new Database(new MockMigrationContext(Target));
            var t = db.AlterTable("Customers");
            t.AlterColumn("NewColumn").DropDefault();
            t.DropColumn("NewColumn");

            string[] sql = Target.GenerateSqlCommands(db).ToArray();
            Assert.AreEqual(2, sql.Length);
            Assert.AreEqual(
                "DECLARE @NewColumnDefaultName VARCHAR(MAX);" + Environment.NewLine +
                "SELECT @NewColumnDefaultName = o2.name " +
                "FROM syscolumns c " +
                "JOIN sysobjects o ON c.id = o.id " +
                "JOIN sysobjects o2 ON c.cdefault = o2.id " +
                "WHERE o.name = 'Customers' AND c.name = 'NewColumn';" + Environment.NewLine +
                "EXEC('ALTER TABLE [Customers] DROP CONSTRAINT ' + @NewColumnDefaultName);",
                sql[0]
            );
            Assert.AreEqual("ALTER TABLE [Customers] DROP COLUMN [NewColumn];", sql[1]);
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:21,代码来源:SqlServerProviderTest.cs

示例9: DatabaseModel3Test

        public void DatabaseModel3Test()
        {
            Database db = new Database(new MockMigrationContext(Target));
            var t = db.AlterTable("Customers");
            t.AlterColumn("Firstname").Index("MyIndex");

            string[] sql = Target.GenerateSqlCommands(db).ToArray();
            Assert.AreEqual(1, sql.Length);
            Assert.AreEqual("CREATE INDEX [MyIndex] ON [Customers] ([Firstname]);", sql[0]);
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:10,代码来源:SqlServerProviderTest.cs

示例10: DatabaseModel2Test

        public void DatabaseModel2Test()
        {
            Database db = new Database(new MockMigrationContext(Target));
            var t = db.AlterTable("Customers");
            t.AddColumn("Firstname", SqlTypes.NVarChar, 32).NotNull();
            t.AlterColumn("Lastname").Rename("Surname");

            string[] sql = Target.GenerateSqlCommands(db).ToArray();
            Assert.AreEqual(2, sql.Length);
            Assert.AreEqual("ALTER TABLE [Customers] ADD [Firstname] NVARCHAR(32) NOT NULL;", sql[0]);
            Assert.AreEqual("EXEC sp_Rename '[Customers].[Lastname]', '[Surname]', 'COLUMN';", sql[1]);
        }
开发者ID:stevencasey,项目名称:NMigrations,代码行数:12,代码来源:SqlServerProviderTest.cs


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