简体   繁体   English

从表Code First Entity Framework 6中删除标识插入

[英]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.

相关问题 使用现有数据库和实体框架的代码优先:当IDENTITY_INSERT设置为OFF时,无法为表中的标识列插入显式值 - Code First With Existing Database, Entity Framework: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF 首先是实体框架代码-创建表并将现有表中的数据插入到新创建的表中, - Entity framework code first - create table and insert data from existing table into newly created one, 实体框架代码优先不会将身份设置为是 - Entity Framework Code First will not set Identity to Yes 为什么实体框架允许我在循环的第一次迭代中插入带有标识主键的表,但之后失败? - Why does Entity Framework allow me to insert into a table with an identity primary key on the first iteration of a loop but fails after? 实体框架核心代码保存前首先更改添加的实体值会导致身份插入错误 - Entity Framework Core Code First changing added entities values before save causes identity insert error 实体框架代码First On Insert方法钩子 - Entity Framework Code First On Insert method hooks 实体框架6代码首先更改从视图到新表的映射 - entity framework 6 code first change mapping from view to new table Entity Framework Code First - 从表中删除相关项 - Entity Framework Code First - Delete related items from a table 实体框架代码优先 - 来自同一个表的两个外键 - Entity Framework Code First - two Foreign Keys from same table 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表'[table]'中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM