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

标签 asp.net asp.net-mvc nhibernate

使用 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/

相关文章:

linq - 编译 NHibernate Linq 表达式

c# - 在ASP.NET Core 2和Entity Framework Core 2中查询表中附近的位置

c# - 如何从 NopCommerce v3.5 的设置表加载记录

c# - 如何使用 NHibernate Criteria API 进行多重连接

jquery - 如何构建 Web 应用程序以使 AJAX 和 DHTML 更容易?

c# - Entity Framework 和整词匹配——分页和过滤结果

nhibernate - S#arp-architecture, NHibernate 多个带有类型 id 的数据库

c# - 是否可以通过http将数据从客户端传输到服务器?

c# - Oracle.DataAccess 错误

asp.net - 集合已修改;枚举操作可能无法执行。但为什么?