title: How to resolve batch update returned unexpected row count error in NHibernate?
body: <p>I have getting following error while inserting the new records in database using NHibernate.</p>
  
  <p><strong><code>{"Batch update returned unexpected row count from update; actual row count: 0; expected: 1"}</code></strong></p>
  
  <p>I have two tables with primary and foreign relationship. I want to inset the records in the both table.: here is the mapping classes</p>
  
  <p>DemoStudentMap.cs</p>
  
  <pre><code> public DemoStudentMap() {
              Table("DEMO_Student");
              Id(t =&gt; t.StudentId).Column("StudentId").GeneratedBy.Identity();
              Map(t =&gt; t.Name, "Name");
              Map(t =&gt; t.Class, "Class");
              Map(t =&gt; t.Board, "Board");
              Map(t =&gt; t.Enabled, "Enabled");
              Map(t =&gt; t.Isdeleted).Column("IsDeleted");
              Map(t =&gt; t.Createddate).Column("CreatedDate");
              Map(t =&gt; t.Lastmodifyby).Column("LastModifyBy").Nullable();
              Map(t =&gt; t.Lastmodifieddate).Column("LastModifiedDate").Nullable();
              References(x =&gt; x.DemoScore).ForeignKey("RollNumber");
            }
  </code></pre>
  
  <p>DemoScoreMap.cs</p>
  
  <pre><code>public DemoScoreMap() {
              Table("DEMO_Score");
              Id(t =&gt; t.rollnumber).Column("RollNumber");
              Map(t =&gt; t.math, "Math");
              Map(t =&gt; t.physics, "Physics");
              Map(t =&gt; t.english, "English");
              Map(t =&gt; t.enabled, "Enabled");
              Map(t =&gt; t.isdeleted).Column("IsDeleted");
              Map(t =&gt; t.createddate).Column("CreatedDate");
              Map(t =&gt; t.lastmodifyby).Column("LastModifyBy").Nullable();
              Map(t =&gt; t.lastmodifieddate).Column("LastModifiedDate").Nullable();
          }
  </code></pre>
  
  <p>I am using Asp.net WebAPI. In the Api controller's Post method i retrieved the values which i want to insert. Here is my ApiController:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Here I got the values in "score" and "student" variables which i want to save in the database. I have the following methods for creating new records which returns bool result as shown in code.</p>
  
  <p>But on the time of saving the data i got the above  mentioned error. Here is the code i inserting. I got same error for both score and student. Here is my code for create:</p>
  
  <p>For Student:</p>
  
  <pre><code> public bool Create(DemoStudent newStudent)
          {
              try
              {
                  _demoStudentRepo.DbContext.BeginTransaction();
                  _demoStudentRepo.SaveOrUpdate(newStudent);
                  _demoStudentRepo.DbContext.CommitTransaction();
              }
              catch
              {
                  return false;
              }
              return true;
          }
  </code></pre>
  
  <p>Fore Score</p>
  
  <pre><code>public bool Create(DemoScore newScore)
          {
              try
              {
                  _demoScoreRepo.DbContext.BeginTransaction();
                  _demoScoreRepo.SaveOrUpdate(newScore);
                  _demoScoreRepo.DbContext.CommitTransaction();
              }
              catch
              {
                  return false;
              }
              return true;
          }
  </code></pre>
  
  <p>Note: when i remove the transaction i does not got this error but still my data is not saved. </p>