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

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

错误:

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


当前回答

即使一切正确,如果Identity列的数据类型不是int或long,也会出现此错误。我有单位列为十进制,虽然我只保存int值(我的坏)。更改数据库和底层模型中的数据类型为我解决了这个问题。

其他回答

使用实体框架与这样的模型有同样的问题(我简化了原始代码):

public class Pipeline
{
  public Pipeline()
  {
     Runs = new HashSet<Run>();
  }

  public int Id {get; set;}
  public ICollection<Run> Runs {get;set;}
}

public class Run
{
  public int Id {get; set;}
  public int RequestId {get; set;}
  public Pipeline Pipeline {get;set;}
}

运行与管道有多对1的关系(一个管道可以运行多次)

在我的RunService中,我注入了dbcontext作为上下文。DbContext有一个Runs DbSet。我在RunService中实现了这个方法:

public async Task<Run> CreateAndInit(int requestId, int pplId)
{
  Pipeline pipeline = await pipelineService.Get(pplId).FirstOrDefaultAsync().ConfigureAwait(false);
  Run newRun = new Run {RequestId = requestId, Pipeline = pipeline};
  context.Runs.Add(newRun);
  await context.SaveChangesAsync().ConfigureAwait(false); // got exception in this line
  return newRun;
}

当方法执行时,我得到了这个异常:

Exception has occurred: CLR/Microsoft.EntityFrameworkCore.DbUpdateException
Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in System.Private.CoreLib.dll: 'An error occurred while updating the entries. See the inner exception for details.'
 Inner exceptions found, see $exception in variables window for more details.
 Innermost exception     Microsoft.Data.SqlClient.SqlException : Cannot insert explicit value for identity column in table 'Pipelines' when IDENTITY_INSERT is set to OFF.

对我来说,解决方案是将对象和关系的创建分开

public async Task<Run> CreateAndInit(int requestId, int pplId)
{
  Pipeline pipeline = await pipelineService.Get(pplId).FirstOrDefaultAsync().ConfigureAwait(false);
  Run newRun = new Run {RequestId = requestId};
  context.Runs.Add(newRun);
  newRun.Pipeline = pipeline; // set the relation separately
  await context.SaveChangesAsync().ConfigureAwait(false); // no exception
  return newRun;
}

EF Core 3.x

引用Leniel Maccaferri,我有一个带有自动递增属性的表,称为ID(原始主键)和另一个属性称为Other_ID(新的主键)。最初ID是主键,但后来Other_ID需要成为新的主键。由于ID正在应用程序的其他部分使用,我不能只是从表中删除它。Leniel Maccaferri解决方案只适用于我之后,我添加了以下片段:

        entity.HasKey(x => x.Other_ID);
        entity.Property(x => x.ID).ValueGeneratedOnAdd();

完整代码片段如下(ApplicationDbContext.cs):

  protected override void OnModelCreating(ModelBuilder builder)
  {
        builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
        base.OnModelCreating(builder);
        builder.Entity<tablename>(entity =>
        {
            entity.HasKey(x => x.Other_ID);
            entity.Property(x => x.ID).ValueGeneratedOnAdd();
            entity.HasAlternateKey(x => new { x.Other_ID, x.ID });                
        });
        
    }

只需删除拖放到.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

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

首先转到所需的表名

步骤2 ->右击表名

选择设计

单击列名

步骤5)进入列属性,然后将No设置为Identity Specification

[注意:插入到显式值后,如果你想,你可以恢复到标识规范为true,然后它会再次生成值]

如果你使用SQL server管理studio你可以使用下面的方法

步骤1) 步骤2)