protected DropDefaultConstraint ( string table, string column, System.Data.Entity.Migrations.Utilities.IndentedTextWriter writer ) : void | ||
table | string | The table to which the constraint applies. |
column | string | The column to which the constraint applies. |
writer | System.Data.Entity.Migrations.Utilities.IndentedTextWriter | The writer to which generated SQL should be written. |
return | void |
protected internal virtual void DropDefaultConstraint(string table, string column, IndentedTextWriter writer)
{
Check.NotEmpty(table, "table");
Check.NotEmpty(column, "column");
Check.NotNull(writer, "writer");
var variable = "@var" + _variableCounter++;
writer.Write("DECLARE ");
writer.Write(variable);
writer.WriteLine(" nvarchar(128)");
writer.Write("SELECT ");
writer.Write(variable);
writer.WriteLine(" = name");
writer.WriteLine("FROM sys.default_constraints");
writer.Write("WHERE parent_object_id = object_id(N'");
writer.Write(table);
writer.WriteLine("')");
writer.Write("AND col_name(parent_object_id, parent_column_id) = '");
writer.Write(column);
writer.WriteLine("';");
writer.Write("IF ");
writer.Write(variable);
writer.WriteLine(" IS NOT NULL");
writer.Indent++;
writer.Write("EXECUTE('ALTER TABLE ");
writer.Write(Escape(Name(table)));
writer.Write(" DROP CONSTRAINT [' + ");
writer.Write(variable);
writer.WriteLine(" + ']')");
writer.Indent--;
}
public void DropDefaultConstraint_checks_its_arguments() { var generator = new SqlServerMigrationSqlGenerator(); var writer = new IndentedTextWriter(new Mock<TextWriter>().Object); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("table"), Assert.Throws<ArgumentException>( () => generator.DropDefaultConstraint(null, "Spektor", writer)).Message); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("column"), Assert.Throws<ArgumentException>( () => generator.DropDefaultConstraint("Regina", null, writer)).Message); Assert.Equal( "writer", Assert.Throws<ArgumentNullException>(() => generator.DropDefaultConstraint("Regina", "Spektor", null)).ParamName); }