每当我尝试通过表单将数据提交到此数据库时,都会引发此异常:-
当IDENTITY_INSERT设置为OFF时,无法在表’ClientDetails’中为标识列插入显式值。
但是,该表单没有字段,因此数据可以输入身份列(PK),所以我不知道为什么会这样。
目前,我正在使用标准的asp.net mvc提交按钮,但最终将其链接到jquery对话框按钮
ClientNo列(异常所指的所述列)具有以下属性
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(); } } }
有谁知道为什么发生这种情况的解决方案或原因?
谢谢
将IsDbGenerated = true添加到ClientNo属性
[Column(Storage="_ClientNo", DbType="Int NOT NULL", **IsDbGenerated=true,** IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)] public int ClientNo