Remove Identity Insert from table Code First Entity Framework 6

c# entity entity-framework-6

Question

I suppose the title says it all. I have to change a table so that the ID PK column is no longer auto generated, so I can physically insert specific values.

Now the table is actually a composite key. Which is as follows;

    [Key,Column("driverId", Order=0)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public override int ID { get; set; }

    [Key,Column("type", Order=1)]
    [ForeignKey("DriverType")]
    public int DriverTypeId { get; set; }
    public virtual DriverType DriverType { get; set; }

Now I have added the DatabaseGenerated attribute and ran a migration which did create the following;

        DropPrimaryKey("dbo.Driver");
        AlterColumn("dbo.Driver", "driverId", c => c.Int(nullable: false));
        AddPrimaryKey("dbo.Driver", new[] { "driverId", "type" });

i.e. the AlterColumn doesnt have the Identity bool in it.

However when I look at the make up of the table I have the following

CREATE TABLE [dbo].[Driver] (
    [driverId]             INT                IDENTITY (1, 1) NOT NULL,

Am I safe to just removd the IDENTITY (1,1)?

1
0
9/29/2015 12:14:40 PM

Popular Answer

To answer you question, no it is not safe to do that.

Add [DatabaseGenerated(DatabaseGeneratedOption.None)] to both parts of the key.

0
9/29/2015 1:21:55 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow