无法为表' ClientDetails'中的标识列插入显式值.当IDENTITY_INSERT设置为OFF时 [英] Cannot insert explicit value for identity column in table 'ClientDetails' when IDENTITY_INSERT is set to OFF

查看:87
本文介绍了无法为表' ClientDetails'中的标识列插入显式值.当IDENTITY_INSERT设置为OFF时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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列(异常所指的所述列)具有以下属性

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

  • 名称-客户编号
  • 类型-整数
  • NULL-否
  • 身份说明-是
  • 是身份-是
  • 增量-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

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆