gpt4 book ai didi

asp.net - 如何解决 NHibernate 中批量更新返回意外行计数错误?

转载 作者:行者123 更新时间:2023-12-01 22:29:20 25 4
gpt4 key购买 nike

使用 NHibernate 在数据库中插入新记录时出现以下错误。

{"批量更新从更新中返回意外行数;实际行数:0;预期: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 Controller 的 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);

在这里,我得到了“分数”和“学生”变量中的值,我想将它们保存在数据库中。我有以下用于创建新记录的方法,这些方法返回 bool 结果,如代码所示。

但是在保存数据时我收到了上述错误。这是我插入的代码。我对分数和学生都犯了同样的错误。这是我的创建代码:

对于学生:

 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;
}

注意:当我删除交易时,我没有收到此错误,但我的数据仍然没有保存。

最佳答案

就我而言,它是身份 key 。它缺少 .GeneratorBy 逻辑。

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

更改为

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

因为它显然无法正确创建标识列。所以这是一个映射问题。希望它能有所帮助。

关于asp.net - 如何解决 NHibernate 中批量更新返回意外行计数错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21042663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com