我正在尝试使用 nHibernate 更新记录。我尝试了几种解决方案,但都没有用(没有显示错误,数据也没有更新)。
第一段代码:
MyRepository rep = new MyRepository(GetCurrentSession());
UserPost post = rep.GetById(id);
post.ValidTo = date;
rep.Update(post);
第二个代码:
ISession session = GetCurrentSession();
MyRepository rep = new MyRepository(GetCurrentSession());
UserPost post = rep.GetById(id);
post.ValidTo = date;
rep.Update(post);
session.Update(post);
session.Transaction.Commit();
session = null;
也许有人有什么建议?
最佳答案
1) 如果您不使用事务,则需要刷新 session :
var post = _session.Load<Post>(id); //assumes this record exists in the db
post.SomeAttribute=somenewvalue;
_session.SaveOrUpdate(post);
_session.Flush;
2) 我没有看到正在启动的交易?您需要启动事务才能提交。
using(var transaction = _session.BeginTransaction()){
_session.SaveOrUpdate(post);
transaction.commit();
}
关于nHibernate 更新不适用于现有实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10414243/