asp.net-mvc – NHibernate – 懒惰地初始化一个角色集合
发布时间:2020-05-23 18:25:41 所属栏目:asp.Net 来源:互联网
导读:我有以下看似简单的场景,但是我仍然很喜欢NHibernate。 在我的控制器上尝试加载编辑操作的以下模型时: 控制器编辑操作: public ActionResult Edit(Guid id){ return View(_repository.GetById(id));} 库: public SomeModel GetById(Guid id){ using (ISes
|
我有以下看似简单的场景,但是我仍然很喜欢NHibernate。 在我的控制器上尝试加载编辑操作的以下模型时: 控制器编辑操作: public ActionResult Edit(Guid id)
{
return View(_repository.GetById(id));
}
库: public SomeModel GetById(Guid id)
{
using (ISession session = NHibernateSessionManager.Instance.GetSession())
return session.Get<SomeModel >(id);
}
模型: public class SomeModel
{
public virtual string Content { get; set; }
public virtual IList<SomeOtherModel> SomeOtherModel { get; set; }
}
我收到以下错误: – 以懒惰的方式初始化一个角色集合:SomeOtherModel,没有会话或会话被关闭 我在这里缺少什么? 解决方法问题是您创建并关闭会话中您的模型GetById方法。 (使用语句关闭会话)会话在整个业务交易中必须可用。有几种方法可以实现这一点。您可以将NHibernate配置为使用会话工厂GetCurrentSession方法。见this post on NHForge或this post on Code Project。 public SomeModel GetById(Guid id)
{
// no using keyword here,take the session from the manager which
// manages it as configured
ISession session = NHibernateSessionManager.Instance.GetSession();
return session.Get<SomeModel >(id);
}
我不用这个我写了自己的交易服务,允许以下内容: using (TransactionService.CreateTransactionScope())
{
// same session is used by any repository
var entity = xyRepository.Get(id);
// session still there and allows lazy loading
entity.Roles.Add(new Role());
// all changes made in memory a flushed to the db
TransactionService.Commit();
}
但是,您实现它,只要业务事务(或系统功能),会话和事务应该生活。除非你不能依赖于事务隔离或者回滚整个事情。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 为什么我需要PUT或DELETE Http Verbs?
- asp.net-mvc – BestPractices:在MVC-Controller中使用多个
- 在asp.net页面中的CSS粘贴页脚
- asp.net – 值得学习经典ASP?
- ASP.NET页面在IE缓存的清除办法
- asp.net – 任何类似于蚂蚁分析器和免费的工具?
- 如何在ASP.NET WebForms中实现TDD
- asp.net-mvc – Asp.Net MVC 2 – 更改PropertyValueRequir
- ASP.NET:在URL中隐藏查询字符串
- asp.net-mvc-3 – ASP.net MVC路由与可选的第一个参数
推荐文章
站长推荐
- asp.net – ADFS 2.0超时以及Freshness Value,To
- asp.net-mvc – 如何使@ Html.EditorFor禁用
- .net – 将数据写入App_Data
- asp.net-mvc – 在Razor web helper中使用html助
- asp.net-mvc – 在ASP.NET MVC Preview 4中使用路
- asp.net – UpdatePanel中的DropDownList
- asp.net – 什么时候应该使用Page.DataBind()和C
- asp.net-mvc – 单个控制器的MVC多个视图
- asp.net – 自定义elmah.axd输出字段
- ASP.NET会话超时测试
热点阅读
