相关实体如下:
public class FieldBookingDateRange : ITimeStamps, ICreatedBy, ISoftDeletable
{
[Key, DatabaseGenerated( DatabaseGeneratedOption.None )]
public int FieldBookingDateRangeID { get; set; }
[Required]
public virtual FieldBooking FieldBooking { get; set; }
public virtual DateRange DateRange { get; set; }
....
}
我有一个数据库列,当前设置为Identity:true。
如果我在其上运行以下自动生成的迁移(这应该在标识上将列设置为false):
public partial class RemovedDBGen_FieldBookingDateRange : DbMigration
{
public override void Up()
{
DropIndex("dbo.FieldBookingDateRanges", new[] { "FieldBookingDateRangeID" });
DropPrimaryKey("dbo.FieldBookingDateRanges");
AlterColumn("dbo.FieldBookingDateRanges", "FieldBookingDateRangeID", c => c.Int(nullable: false, identity: false));
AddPrimaryKey("dbo.FieldBookingDateRanges", "FieldBookingDateRangeID");
CreateIndex("dbo.FieldBookingDateRanges", "FieldBookingDateRangeID");
}
public override void Down()
{
DropIndex("dbo.FieldBookingDateRanges", new[] { "FieldBookingDateRangeID" });
DropPrimaryKey("dbo.FieldBookingDateRanges");
AlterColumn("dbo.FieldBookingDateRanges", "FieldBookingDateRangeID", c => c.Int(nullable: false, identity: true));
AddPrimaryKey("dbo.FieldBookingDateRanges", "FieldBookingDateRangeID");
CreateIndex("dbo.FieldBookingDateRanges", "FieldBookingDateRangeID");
}
}
它没有做任何事......但不会失败。
为什么这不起作用?
SQL -Verbose输出
IF EXISTS (
SELECT name FROM sys.indexes
WHERE
name = N'IX_FieldBookingDateRangeID' AND
object_id = object_id(N'[dbo].[FieldBookingDateRanges]', N'U')
)
DROP INDEX [IX_FieldBookingDateRangeID] ON [dbo].[FieldBookingDateRanges]
ALTER TABLE [dbo].[FieldBookingDateRanges] DROP CONSTRAINT [PK_dbo.FieldBookingDateRanges]
ALTER TABLE [dbo].[FieldBookingDateRanges] ALTER COLUMN [FieldBookingDateRangeID] [int] NOT NULL
ALTER TABLE [dbo].[FieldBookingDateRanges] ADD CONSTRAINT [PK_dbo.FieldBookingDateRanges] PRIMARY KEY ([FieldBookingDateRangeID])
CREATE INDEX [IX_FieldBookingDateRangeID] ON [dbo].[FieldBookingDateRanges]([FieldBookingDateRangeID])
答案 0 :(得分:1)
我认为一旦设置了列,就无法从列中删除标识属性。但是,您可以修改自动生成的迁移以实现变通方法。
你可以在这里找到一些例子:
Remove Identity from a column in a table
如果您需要保留数据,但删除了IDENTITY列,则需要:
- 创建新列
- 将数据从现有IDENTITY列传输到新列
- 删除现有的IDENTITY列。