当我执行下面的脚本时,我有以下错误。错误是关于什么,如何解决?

Insert table(OperationID,OpDescription,FilterID)
values (20,'Hierachy Update',1)

错误:

服务器:Msg 544,级别16,状态1,线路1 当IDENTITY_INSERT设置为OFF时,无法为表'table'中的标识列插入显式值。


当前回答

只需删除拖放到.dbml文件中的表,然后重新拖动它们。清洁方案>重建方案>重建方案。

这对我很管用。

我没有做我自己的表格,我是用VS和SSMS,我遵循这个链接为ASP。净身份:https://learn.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project

其他回答

要非常小心地将IDENTITY_INSERT设置为ON。这是一种糟糕的实践,除非数据库处于维护模式并设置为单用户。这不仅会影响您的插入,还会影响试图访问表的其他人的插入。

为什么要在单位字段中输入一个值?

只需删除拖放到.dbml文件中的表,然后重新拖动它们。清洁方案>重建方案>重建方案。

这对我很管用。

我没有做我自己的表格,我是用VS和SSMS,我遵循这个链接为ASP。净身份:https://learn.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project

我通过每次向数据库添加任何东西时创建一个新对象来解决这个问题。

我使用asp.net core 5.0,我得到这个错误。我得到这个错误,因为我添加了另一个数据并触发了另一个.SaveChanges()方法,如下所示:

 _unitOfWorkVval.RepositoryVariantValue.Create(variantValue);
 int request = HttpContext.Response.StatusCode;
 if (request == 200)
 {
     int tryCatch = _unitOfWorkCVar.Complete();
     if (tryCatch != 0)
     {
         productVariant.CategoryVariantID = variantValue.CategoryVariantID;
         productVariant.ProductID = variantValue.ProductID;
         productVariant.CreatedDate = DateTime.Now;
         _unitOfWorkProductVariant.RepositoryProductVariant.Create(productVariant);
         _unitOfWorkVval.RepositoryVariantValue.Create(variantValue);
         int request2 = HttpContext.Response.StatusCode;
         if(request==200)
         {
             int tryCatch2=_unitOfWorkProductVariant.Complete();//The point where i get that error
         }///.......

您正在为OperationId插入值,这是一个标识列。

您可以像这样在表上打开标识插入,以便您可以指定自己的标识值。

SET IDENTITY_INSERT Table1 ON

INSERT INTO Table1
/*Note the column list is REQUIRED here, not optional*/
            (OperationID,
             OpDescription,
             FilterID)
VALUES      (20,
             'Hierachy Update',
             1)

SET IDENTITY_INSERT Table1 OFF