Identity column is not being modified by the Entity framework
The corresponding object looks like this:
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 for identification):
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 it doesn't fail.
Any ideas why this isn't working?
SQL output -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])
+3
source to share
1 answer
I think you cannot remove the identity property from the column after setting it. However, you can modify the auto-generated migration to implement a workaround.
You can find some examples here:
-
If you need to keep the data but remove the IDENTITY column, you need to:
- Create a new column
- Transferring data from an existing IDENTITY column to a new column
- Override the existing IDENTITY column.
+1
source to share