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

Cannot insert explicit value for identity column in table #39;ClientDetails#39; when IDENTITY_INSERT is set to OFF(当 IDENTITY_INSERT 设置为 OFF 时,无法为表“ClientDetails中的标识列插入显式值)
本文介绍了当 IDENTITY_INSERT 设置为 OFF 时,无法为表“ClientDetails"中的标识列插入显式值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

每当我尝试通过表单向此数据库提交数据时,都会抛出此异常:-

I get this exception thrown whenever I try and submit data through a form to this database :-

  • 异常

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

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

However, the form doesn't have a field so data can enter the identity column (PK) so I'm at a loss as to why this is occuring.

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

At the moment I'm using the standard asp.net mvc submit button but I will link it eventually to a jquery dialog button

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

The ClientNo Column which is the said column the exception is referring to has the following attributes

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

ClientNo 有 900 以后的数据等

The ClientNo has data 900 onwards etc

当客户端表单没有在表单中输入数据时也会抛出这个异常

This exception is also thrown when the client form has no data entered in form

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

Its thrown on a DataCOntext.SubmitChanges() method

[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();
                }
            }
        }

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

Does anyone know a solution or reason as to why this is occuring?

谢谢

推荐答案

将 IsDbGenerated=true 添加到 ClientNo 属性

Add IsDbGenerated=true To ClientNo Property

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

这篇关于当 IDENTITY_INSERT 设置为 OFF 时,无法为表“ClientDetails"中的标识列插入显式值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)