2014-01-10 13 views
2

我在使用NHibernate在數據庫中插入新記錄時遇到以下錯誤。如何解決批量更新在NHibernate中返回意外的行計數錯誤?

{"Batch update returned unexpected row count from update; actual row count: 0; expected: 1"}

我有一次和外國關係的兩個表。我想在這兩個表中的插圖中的記錄:這裏是映射類

DemoStudentMap.cs

public DemoStudentMap() { 
      Table("DEMO_Student"); 
      Id(t => t.StudentId).Column("StudentId").GeneratedBy.Identity(); 
      Map(t => t.Name, "Name"); 
      Map(t => t.Class, "Class"); 
      Map(t => t.Board, "Board"); 
      Map(t => t.Enabled, "Enabled"); 
      Map(t => t.Isdeleted).Column("IsDeleted"); 
      Map(t => t.Createddate).Column("CreatedDate"); 
      Map(t => t.Lastmodifyby).Column("LastModifyBy").Nullable(); 
      Map(t => t.Lastmodifieddate).Column("LastModifiedDate").Nullable(); 
      References(x => x.DemoScore).ForeignKey("RollNumber"); 
      } 

DemoScoreMap.cs

public DemoScoreMap() { 
      Table("DEMO_Score"); 
      Id(t => t.rollnumber).Column("RollNumber"); 
      Map(t => t.math, "Math"); 
      Map(t => t.physics, "Physics"); 
      Map(t => t.english, "English"); 
      Map(t => t.enabled, "Enabled"); 
      Map(t => t.isdeleted).Column("IsDeleted"); 
      Map(t => t.createddate).Column("CreatedDate"); 
      Map(t => t.lastmodifyby).Column("LastModifyBy").Nullable(); 
      Map(t => t.lastmodifieddate).Column("LastModifiedDate").Nullable(); 
     } 

我使用Asp.net的WebAPI。在Api控制器的Post方法中,我檢索了要插入的值。這裏是我的ApiController:

DemoScoreViewModel newScore = new DemoScoreViewModel(); 
DemoScore score = newScore.ConvertDemoScoreViewModelToDemoS(newStudent, _crudStatusCreate); 
bool resultScore = _demoScoreTask.Create(score); 
DemoStudent student = newStudent.ConvertDemoStudentViewModelToDemoStudent(newStudent, score, _crudStatusCreate); 
bool result = _demoStudentTask.Create(student); 

在這裏,我得到了我想要在數據庫中保存的「分數」和「學生」變量的值。我有以下創建新記錄的方法,它返回代碼中顯示的布爾結果。

但是,在保存數據時,我得到了上述錯誤。這是我插入的代碼。得分和學生都有同樣的錯誤。這裏是我的代碼創建:

對於學生:

public bool Create(DemoStudent newStudent) 
     { 
      try 
      { 
       _demoStudentRepo.DbContext.BeginTransaction(); 
       _demoStudentRepo.SaveOrUpdate(newStudent); 
       _demoStudentRepo.DbContext.CommitTransaction(); 
      } 
      catch 
      { 
       return false; 
      } 
      return true; 
     } 

富勒分數

public bool Create(DemoScore newScore) 
     { 
      try 
      { 
       _demoScoreRepo.DbContext.BeginTransaction(); 
       _demoScoreRepo.SaveOrUpdate(newScore); 
       _demoScoreRepo.DbContext.CommitTransaction(); 
      } 
      catch 
      { 
       return false; 
      } 
      return true; 
     } 

注:當我刪除事務我不得到這個錯誤,但仍然是我的數據不會保存。

+0

你有沒有嘗試過這裏的建議:http://stackoverflow.com/questions/4083368/how-to-solve-batch-update-returned-unexpected-row-count-from-update-actual-row?rq = 1 – Tallmaris

回答

2

終於得到了問題。錯誤在Mapping中。

public DemoScoreMap() { 
      Table("DEMO_Score"); 
      Id(t=>t.rollnumber).Column("RollNumber").GeneratedBy.Assigned(); 
      Map(t => t.math, "Math"); 
      Map(t => t.physics, "Physics"); 
      Map(t => t.english, "English"); 
      Map(t => t.enabled, "Enabled"); 
      Map(t => t.isdeleted).Column("IsDeleted"); 
      Map(t => t.createddate).Column("CreatedDate"); 
      Map(t => t.lastmodifyby).Column("LastModifyBy").Nullable(); 
      Map(t => t.lastmodifieddate).Column("LastModifiedDate").Nullable(); 
     } 
+1

老實說,我在這裏感到驚訝。通常情況下,如果應該分配標識,NHibernate將發出沒有Id(RollNumber列)的INSERT,我們應該得到異常:*嘗試將null插入到非空列*中。 IE不批迴來......並且也請記住,一旦你使用了'Assigned'你不應該使用SaveOrUpdate。 NHibernate在這種情況下很難鑑別assinged vs not-assigned的值......如下所述;)NHibernate無論如何都是好運.. –

+0

Im使用Automapping並得到相同的錯誤:S –

4

這裏的問題隱藏在一個事實中,即SaveOrUpdate()被調用。 NHibernate出於某些原因(稍後討論)決定調用「UPDATE」。但是因爲我們正在創建新的實例,所以更新的行數...爲0.期待1

可能是什麼原因? DemoScoreDemosStudent在缺省值id值和UnsavedValue設置中確實存在incnosis。

即NHibernate預計ID == 0意味着,新...而任何其他值(甚至負)將被視爲現有的實體...應該更新。

因此,檢查什麼值被分配給該ID的_demoScoreTask.Create(score);

的內部的默認設置(0表示新)可以在映射,例如可以調整.UnsavedValue(-1)

注意:原因,爲什麼版本沒有事務沒有引發異常,那Flush()沒有被調用。請檢查9.6. Flush。例如,我們可以將sess.FlushMode = FlushMode.Commit;更改爲Auto,但Commit是合適的。

+0

我調試的創建功能,我發現,ID爲0。根據我這是正確的。然後它給了我什麼錯誤。 ((SharpArch.Domain.DomainModel.EntityWithTypedId )(newScore))。ID爲0 –

+0

嘗試顯式設定的'UnsavedValue(0)'。它似乎被設置爲別的(-1)。我幾乎可以肯定,因爲SaveOrUpdate不更新...... *(我會說)*,以及必須意味着0不表示爲未保存的價值。 *(注意:唯一的另一種選擇可能是Insert返回0行...而不是1,但在我看來,這太複雜了,例如某些觸發器或設置...因此最類似於上述情況) –

+0

其中我必須寫UnsavedValue(0)。 –

4

在我的情況下,它是身份密鑰。它缺少.GeneratedBy邏輯。

Table("JobDetailsBlob"); 
Id(x => x.Id, "JobDetailId"); 

改爲

Table("JobDetailsBlob"); 
Id(x => x.Id, "JobDetailId").GeneratedBy.Assigned(); 

,因爲它顯然不能正確地創建標識列。所以這是一個映射問題。希望它可以幫助。

相關問題