【问题标题】:Cannot insert explicit value for identity column in table 'ClientDetails' when IDENTITY_INSERT is set to OFF当 IDENTITY_INSERT 设置为 OFF 时,无法在表“ClientDetails”中插入标识列的显式值
【发布时间】:2011-08-26 08:32:48
【问题描述】:

每当我尝试通过表单向该数据库提交数据时都会引发此异常:-

  • 例外

    当 IDENTITY_INSERT 设置为 OFF 时,无法为表“ClientDetails”中的标识列插入显式值。

但是,表单没有字段,因此数据可以输入身份列 (PK),所以我不知道为什么会发生这种情况。

目前我正在使用标准的 asp.net mvc 提交按钮,但我最终会将它链接到一个 jquery 对话框按钮

异常所指的列的ClientNo列具有以下属性

  • 名称 - ClientNo
  • 类型 - 整数
  • NULLS - 否
  • 身份规范 - 是
  • 是身份 - 是
  • 增量 - 1
  • 种子 - 1

ClientNo 有 900 以后的数据等

当客户端表单没有输入数据时也会抛出此异常

它在 DataCONtext.SubmitChanges() 方法上抛出

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create1(ClientDetail client)
        {
            if(ModelState.IsValid)
            { 
            client = new ClientDetail();



                UpdateModel(client);
                //Debug Code
                System.Diagnostics.Debug.WriteLine("AddNewClient Create1");
                repo.AddNewClient(client);
                //Debug Code
                System.Diagnostics.Debug.WriteLine("Save Create1");


                // System.Data.SqlClient.SqlException thrown at this line  
                repo.Save();

                //Debug Code - never reached
                System.Diagnostics.Debug.WriteLine("Saved Changes");


              //  return RedirectToAction("Details", new { id = client.ClientNo });




            }
            return View(client);
        }

public void AddNewClient(ClientDetail client)
       {    
            System.Diagnostics.Debug.WriteLine("Start Insert On Submit");

            db.ClientDetails.InsertOnSubmit(client);
            System.Diagnostics.Debug.WriteLine("Finish Insert On Submit");
       }

public void Save()
        {
            System.Diagnostics.Debug.WriteLine("In Save Method");
            db.GetChangeSet();
            System.Diagnostics.Debug.WriteLine("Got ChangeSet"); 
            db.SubmitChanges();
            System.Diagnostics.Debug.WriteLine("Exit Save Method");
        }
Is this the query everyone is talking about

[Column(Storage="_ClientNo", DbType="Int NOT NULL", IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)]
        public int ClientNo
        {
            get
            {
                return this._ClientNo;
            }
            set
            {
                if ((this._ClientNo != value))
                {
                    this.OnClientNoChanging(value);
                    this.SendPropertyChanging();
                    this._ClientNo = value;
                    this.SendPropertyChanged("ClientNo");
                    this.OnClientNoChanged();
                }
            }
        }

有没有人知道为什么会发生这种情况的解决方案或原因?

谢谢

【问题讨论】:

  • 如何向数据库“提交”数据?
  • 当您想向种子列添加值并且 identity_insert 关闭时会生成此错误。那么如何将数据保存到数据库中呢?
  • 如果你能发布查询就好了。
  • @Abdul Muq - 使用从 linq 到 Sql 类的 SubmitChanges 方法
  • 我似乎无法正确处理代码块,但 Linq-Sql 中的查询是

标签: c# asp.net sql linq-to-sql asp.net-mvc-2


【解决方案1】:

将 IsDbGenerated=true 添加到 ClientNo 属性

[Column(Storage="_ClientNo", DbType="Int NOT NULL", **IsDbGenerated=true,** IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)] 
public int ClientNo

【讨论】:

  • +1 用于在正确格式化代码之前知道答案
  • 这个答案不再起作用了!为什么?我收到此错误:“System.ComponentModel.DataAnnotations.ColumnAttribute”不包含“IsPrimaryKey”的定义
  • @Tman 确保您使用的是 System.Data.Linq.Mapping.ColumnAttribute,而不是 System.ComponentModel.DataAnnotations.ColumnAttribute
  • 它说“System.Data.Linq.Mapping.ColumnAttribute 是一种类型而不是命名空间”
  • @Tman 你的代码中有一堆 nonono 。您错误地添加了 using 语句。此外,您不应手动更改自动生成的文件。最好先阅读一些关于实体框架和 linq2sql 的文档。我猜你应该使用部分类来应用该属性
【解决方案2】:

您不能提供要作为插入的一部分插入到标识列中的值。它们作为INSERT 操作的原子部分自动生成。这可以使用SET IDENTITY_INSERT 命令在每个表的基础上暂时禁用。

要么,您应该更改数据库 shcema,但我不建议这样做或更好,您不应该提供值,或者为插入中的 Client_NO 列提供 NULL 值。

在 SQL 中,您可以使用 SCOPE_IDENTITY() 函数来获取自动生成的值。模型将自动使用此值更新。

使用archil 的答案告诉模型不要在生成的选择中传递此列。

[Column(Storage="_ClientNo",
    DbType="Int NOT NULL", 
    IsDbGenerated=true,
    IsPrimaryKey=true, 
    UpdateCheck=UpdateCheck.Never)]  
public int ClientNo

注意 isDBGenerated 的版本到 arrtributes。

【讨论】:

    猜你喜欢
    • 2016-05-13
    • 2016-08-18
    • 2019-03-10
    • 2010-11-22
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多