简体   繁体   English

出现错误:当IDENTITY_INSERT设置为OFF时,无法在表'Table'中为标识列插入显式值

[英]Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF

I have simple DataTable 我有简单的DataTable

CREATE TABLE [dbo].[Table] (
[Id]   INT        IDENTITY (1, 1) NOT NULL,
[Name] NCHAR (10) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

With one record, Name="Test" Id get's automatic 0. 通过一条记录, Name="Test" Id get自动为0。

When I am using basic create operation 当我使用基本的创建操作时

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Table table)
{
    if (ModelState.IsValid)
    {
        db.Table.Add(table);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(table);
}

I get error: 我得到错误:

Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF. 当IDENTITY_INSERT设置为OFF时,无法在表'Table'中为标识列插入显式值。

What I am doing wrong? 我做错了什么? How to fix this error? 如何解决这个错误?

Table class 表类

public partial class Table
{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
}

You need to set IsDbGenerated to true . 您需要将IsDbGenerated设置为true Add System.Data.Linq to your references then: System.Data.Linq添加到您的引用中,然后:

[System.Data.Linq.Mapping.Column(Storage = "Id", DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public int Id { get; set; }

It smells like the table that you are retrieving from the argument ( Table table ) is setting the property Id to other value than 0 . 闻起来像是您从参数( Table table )中检索的Table table将属性Id设置为0以外的其他值。

If that's the case, be sure that you set the Id to 0 before db.Table.Add(table); 如果是这种情况,请确保在db.Table.Add(table);之前将Id设置为0 db.Table.Add(table);

If you want to update the item if it exists, then you need to check if the Id is 0 before adding it. 如果要更新该项目(如果存在),则需要在添加之前检查Id是否为0

if(table.Id == 0)
   db.Table.Add(table);
db.SaveChanges();
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

This worked for me. 这对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当IDENTITY_INSERT设置为OFF时,获取“无法在表'OrderPromo'中为标识列插入显式值”。 在桌子上没有身份 - Getting 'Cannot insert explicit value for identity column in table 'OrderPromo' when IDENTITY_INSERT is set to OFF.' on table with NO Identity SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 [表名] 中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表'[table]'中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF IDENTITY_INSERT设置为OFF时,EF无法为表“ PATIENT DONOR”中的标识列插入显式值 - EF Cannot insert explicit value for identity column in table 'PATIENT DONOR' when IDENTITY_INSERT is set to OFF IDENTITY_INSERT设置为OFF时,1:1映射并且无法为表'DivisionParticipant'中的Identity列插入显式值 - 1:1 mapping and Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,C#无法为表“出租”中的标识列插入显式值 - C# Cannot insert explicit value for identity column in table “Rentals” when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在TABLE中为Identity列插入显式值; .NET Core 2.1 - Cannot insert explicit value for identity column in TABLE when IDENTITY_INSERT is set to OFF; .NET Core 2.1 当IDENTITY_INSERT设置为OFF时,无法在表'ClientDetails'中为identity列插入显式值 - Cannot insert explicit value for identity column in table 'ClientDetails' when IDENTITY_INSERT is set to OFF “当IDENTITY_INSERT设置为OFF”时,无法使用复合键为表中的标识列插入显式值 - “Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF” with composite key 当 IDENTITY_INSERT 设置为 OFF 时,无法在表“消息”中插入标识列的显式值 - Cannot insert explicit value for identity column in table 'Messages' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM