NHibernate 异常

NHibernate.StaleStateException: Batch update returned unexpected row count from update; actual row count: 0; expected: 1

1.这个错的导致原因有许多,笔者这里的原因是因为更新不存在的数据导致。

场景:将几个SQL动作包在事务中执行,

Customer_Mod customer = new Customer_Mod
{
    CustomerID =1,
    KemuID = model.KemuID,
    ZhangtaoID = model.ZhangtaoID,
    NCName = model.NCName,
    MaterialTypes = model.MaterialTypes
};

var m = session.Merge(customer);
session.Flush();
var sqlDel = "delete from [dbo].[Sys_User] where UserID=1" ;
ISQLQuery query = session.CreateSQLQuery(sqlDel);
var n = query.ExecuteUpdate();

错误就是在第三步的session.Flush() 报出的,因为这里拿到的userID实际上是已经删除的那条User数据,但是整个操作包含在事务中,所以这种更新操作是不允许的

ISQLQuery query = session.CreateSQLQuery("SELECT isnull(max(UserID),0) +1 AS C FROM dbo.Sys_User").AddScalar("C", NHibernateUtil.Int32);
var n = query.UniqueResult();
var _userid = Convert.ToInt32(n.ToString());

Sys_User_Mod user = new Sys_User_Mod
{
    UserID = _userid,
    UserName = item.UserName,
};

var m1 = session.Merge(user);
session.Flush();

 

posted @ 2018-05-18 13:30  竹殇  阅读(139)  评论(0编辑  收藏  举报