在删除列的迁移操作期间,如何在尝试删除列之前生成SQL以检查列的存在?
对于drop column操作,Entity框架当前生成这样的sql以删除列:
// Migration Operation:
DropColumn("dbo.Table", "Column");
// TSQL generated:
// Dependency management logic ...
ALTER TABLE [dbo].[Table] DROP COLUMN [Column]
如何更改SQL以首先检查列的存在:
// Migration Operation:
DropColumn("dbo.Table", "Column");
// TSQL desired:
IF EXISTS (SELECT * FROM sys.columns WHERE object_id = Object_id('dbo.Table') AND name = 'Column')
BEGIN
// Dependency management logic ...
ALTER TABLE [dbo].[Table] DROP COLUMN [Column]
END
我知道可以通过继承SqlServerMigrationSqlGenerator
来自定义迁移SQL。我尝试这样做无法将IF
块中的默认丢弃列逻辑包装起来。见下面的例子:
public class CustomSqlServerMigrationSqlGenerator: SqlServerMigrationSqlGenerator
{
/// <summary>
/// Drop column only if it exists.
/// </summary>
protected override void Generate(System.Data.Entity.Migrations.Model.DropColumnOperation dropColumnOperation)
{
using (var writer = Writer())
{
writer.WriteLine(
"IF EXISTS (SELECT * FROM sys.columns WHERE object_id = Object_id('{0}') AND name = '{1}')",
dropColumnOperation.Table,
dropColumnOperation.Name);
writer.WriteLine("BEGIN");
Statement(writer);
}
// Default drop column logic
base.Generate(dropColumnOperation);
using (var writer = Writer())
{
writer.WriteLine("END");
Statement(writer);
}
}
}
来源:
答案 0 :(得分:0)
如果您已正确配置CustomSqlServerMigrationSqlGenerator
,则在执行Update-Database
时,您应该会遇到以下错误消息:
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 'BEGIN'.
问题是您已经构造并执行了3条局部语句,默认情况下对Statement()
的调用是作为单个批处理操作执行的,并且直到实际的 DropColumn,您的批处理才是有效的语法该语句中包含em>语句和 End 。
由于基本实现不允许我们通过文本编写器(我们需要的方法标记为protected
),我们被迫要做的是完全忽略基本实现:
/// <summary>
/// Drop column only if it exists.
/// </summary>
/// <remarks>This helps when we're stuffed up a previous migration or someone has already executed the drop in the DB direct.</remarks>
protected override void Generate(System.Data.Entity.Migrations.Model.DropColumnOperation dropColumnOperation)
{
using (var writer = Writer())
{
writer.WriteLine(
"IF EXISTS (SELECT * FROM sys.columns WHERE object_id = Object_id('{0}') AND name = '{1}')",
dropColumnOperation.Table,
dropColumnOperation.Name);
writer.WriteLine("BEGIN");
// Base Implementation, increase the indentation
writer.Indent++;
writer.WriteLine("ALTER TABLE {0}", Quote(dropColumnOperation.Table));
writer.WriteLine("DROP COLUMN {0}", Quote(dropColumnOperation.Name));
writer.Indent--;
writer.WriteLine("END");
Statement(writer);
}
}
如果没有看到当前错误,可能是CustomSqlServerMigrationSqlGenerator
未正确注册,请确保在Configuration.cs的Configuration类的构造函数中进行了 set 设置。 :
public Configuration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = false;
// Register the Customized SQL Generator to use
this.SetSqlGenerator("System.Data.SqlClient", new CustomSqlServerMigrationSqlGenerator());
}
如果需要调试此过程,请遵循advice in this post(与本场景类似的场景),可以在生成器类的构造函数中放置一个断点:
public class CustomSqlServerMigrationSqlGenerator: SqlServerMigrationSqlGenerator
{
public CustomSqlServerMigrationSqlGenerator()
: base()
{
if (!System.Diagnostics.Debugger.IsAttached)
System.Diagnostics.Debugger.Launch();
}
...
}