3

The relevant Entity is as follows:

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; }

    ....
}

I have a database column that is currently set to Identity: true.

If i run the following auto generated migration on it (This should be setting the column to false on identity):

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");
    }
}

It doesn't do anything... But doesn't fail.

Any ideas why this isn't working?

SQL -Verbose output

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])
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233

1 Answers1

1

I think you can't remove the identity property from a column once it's set. However you might be able to modify the automatically generated migration to implement a workaround.

You can find some examples here:

Community
  • 1
  • 1
jjj
  • 4,822
  • 1
  • 16
  • 39