[英]Remove Identity Insert from table Code First Entity Framework 6
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. 我必须更改一个表,以便不再自动生成ID PK列,因此可以物理插入特定值。
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; 现在,我添加了DatabaseGenerated属性并运行了一个迁移,该迁移确实创建了以下内容:
DropPrimaryKey("dbo.Driver");
AlterColumn("dbo.Driver", "driverId", c => c.Int(nullable: false));
AddPrimaryKey("dbo.Driver", new[] { "driverId", "type" });
ie the AlterColumn doesnt have the Identity bool in it. 即,AlterColumn中没有Identity bool。
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)? 我可以安全删除IDENTITY(1,1)吗?
To answer you question, no it is not safe to do that. 要回答您的问题,不,这样做是不安全的。
Add [DatabaseGenerated(DatabaseGeneratedOption.None)] to both parts of the key. 将[DatabaseGenerated(DatabaseGeneratedOption.None)]添加到密钥的两个部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.