本文介绍了代码优先迁移期间更新数据库期间出错。 - “当IDENTITY_INSERT设置为OFF时,无法在表'表名'中为标识列插入显式值。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public override void Up()
  {
    Sql("INSERT INTO Workouts (Id, Name, Description) VALUES (1, 'HIIT', 'High-Intensity Interval Training​')");
    Sql("INSERT INTO Workouts (Id, Name, Description) VALUES (2, 'Yoga', 'Flexibility, relaxation, less stress')");
           
 }





我的尝试:



我首先运行代码迁移。在包管理器控制台的update-database期间,我收到了这个错误。



当IDENTITY_INSERT设置为OFF时,无法在表'表名'中为标识列插入显式值。



What I have tried:

I run the code first migration. during update-database in package manager console, i got this error.

"Cannot insert explicit value for identity column in table 'table name' when IDENTITY_INSERT is set to OFF."

推荐答案

Quote:

无法在标识列中插入显式值当IDENTITY_INSERT设置为OFF时,表'表名'。

"Cannot insert explicit value for identity column in table 'table name' when IDENTITY_INSERT is set to OFF."



直接发行。

Id 是一个标识列,你不能明确地直接向表中插入标识值,

你将不得不将 IDENTITY_INSERT 设置为 ON 以插入id值。


straight forward issue.
Id is an identity column, you cannot explicity insert identity value to the table directly,
You will have to set the IDENTITY_INSERT to ON to insert the id value.

SET IDENTITY_INSERT Workouts ON
 -- insert  the data 
INSERT INTO Workouts (Id, Name, Description) VALUES (1, 'HIIT', 'High-Intensity Interval Training​')   
 SET IDENTITY_INSERT Workouts OFF     


这篇关于代码优先迁移期间更新数据库期间出错。 - “当IDENTITY_INSERT设置为OFF时,无法在表'表名'中为标识列插入显式值。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 22:55