I have getting following error while inserting the new records in database using NHibernate.
{"Batch update returned unexpected row count from update; actual row count: 0; expected: 1"}
I have two tables with primary and foreign relationship. I want to inset the records in the both table.: here is the mapping classes
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();
}
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:
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);
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.
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:
For Student:
public bool Create(DemoStudent newStudent)
{
try
{
_demoStudentRepo.DbContext.BeginTransaction();
_demoStudentRepo.SaveOrUpdate(newStudent);
_demoStudentRepo.DbContext.CommitTransaction();
}
catch
{
return false;
}
return true;
}
Fore Score
public bool Create(DemoScore newScore)
{
try
{
_demoScoreRepo.DbContext.BeginTransaction();
_demoScoreRepo.SaveOrUpdate(newScore);
_demoScoreRepo.DbContext.CommitTransaction();
}
catch
{
return false;
}
return true;
}
Note: when i remove the transaction i does not got this error but still my data is not saved.