- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试将我的“公司”实体保存在我的 mvc 应用程序中时出现以下错误
具有相同标识符值的不同对象已与 session 关联:2,实体:
我正在使用 IOC 容器
private class EStoreDependencies : NinjectModule
{
public override void Load()
{
Bind<ICompanyRepository>().To<CompanyRepository>().WithConstructorArgument("session",
NHibernateHelper.OpenSession());
}
}
public class CompanyRepository : ICompanyRepository
{
private ISession _session;
public CompanyRepository(ISession session)
{
_session = session;
}
public void Update(Company company)
{
using (ITransaction transaction = _session.BeginTransaction())
{
_session.Update(company);
transaction.Commit();
}
}
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
const string SessionKey = "MySession";
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(UserProfile).Assembly);
configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName,
System.Environment.MachineName);
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
var context = HttpContext.Current;
//.GetCurrentSession()
if (context != null && context.Items.Contains(SessionKey))
{
//Return already open ISession
return (ISession)context.Items[SessionKey];
}
else
{
//Create new ISession and store in HttpContext
var newSession = SessionFactory.OpenSession();
if (context != null)
context.Items[SessionKey] = newSession;
return newSession;
}
}
}
[HttpPost]
public ActionResult Edit(EStore.Domain.Model.Company company)
{
if (company.Id > 0)
{
_companyRepository.Update(company);
_statusResponses.Add(StatusResponseHelper.Create(Constants
.RecordUpdated(), StatusResponseLookup.Success));
}
else
{
company.CreatedByUserId = currentUserId;
_companyRepository.Add(company);
}
var viewModel = EditViewModel(company.Id, _statusResponses);
return View("Edit", viewModel);
}
最佳答案
我知道这有点晚了,您可能已经找到了解决方案,但也许其他人可以从中受益...
当您更新保存在缓存中的实体实例时,会从 nHibernate 引发此错误。基本上 nHibernate 将您的对象在加载后存储在缓存中,因此下一次调用将从缓存中获取它。如果更新缓存中存在的实例,nHibernate 会抛出此错误,否则可能会导致脏读和加载对象旧副本时发生冲突。
要解决此问题,您需要使用 Evict 方法从缓存中删除对象,例如:
public ActionResult Edit(EStore.Domain.Model.Company company)
{
if (company.Id > 0)
{
**ISession.Evict(company);**
_companyRepository.Update(company);
关于asp.net-mvc - 实体的 nhibernate : a different object with the same identifier value was already associated with the session: 2,:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983749/
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我有四个模型类: class Group :event_producer end class PersonalBlog :event_producer end class Event true
将应用程序从Rails 4.2.9升级到Rails 5.2.1。 通过大部分令人讨厌的部分,更新了依赖关系和诸如此类的东西,最终使应用程序在console中运行,现在尝试访问server上的页面。加载
我的 EntityDefinition 中有一个关联: ... class ParentEntityDefinition extends EntityDefinition { ...
我不确定为什么这个关联无效 class Tag < ActiveRecord::Base has_and_belongs_to_many :routes end class Route < Act
我有以下关联。 PropertyOwner 是一个连接模型,它属于一个属性(property),多态地属于一个所有者,在下面的例子中是一个 ForeclosureDefense。一切正常,直到我拥有
我有一份有很多发票的工作,一张发票属于一份工作。我想查询第一张发票在某个日期范围内的工作。我有这个查询: @jobs = Job.joins(:invoices). where("invoices
我有这样的关系:用户可以拥有零只或一只狗,但狗必须属于某人。 # dog.rb class Dog { joins(:dog) } # To get records without a dog,
在我的 Rails 4 应用程序中,我有以下模型: User has_many :administrations has_many :calendars, through: :administrati
我见过的所有示例,包括文档都建议按关联过滤应使用以下语法 [contrived exampled] User.findAndCountAll({ include: [ {
我有一个下拉列表(HTML 选择框),它从这个 MySQL 查询中获取值: "SELECT cdID, cdTitle FROM CD ORDER BY cdID" 然后将结果存储在关联数组中,然后将
我是 Ruby ON Rails 新手,我的应用有 3 个模型 class User < ActiveRecord::Base has_one :user_hobby has_one :user_
我有三个模型,每个模型都有以下关联: class Model1 :model1 # will this work? is there any way around this? end class
我有一个带有帖子和标签的数据库。我想找到的帖子,只包含一个标签与一个特定的TagID,根本没有其他标签。下面的代码可以工作,但它需要服务器手动过滤结果,这将使分页变得困难。有谁有可以做到这一点的直接查
任何人都知道如何避免 PLS-00312 错误? “PLS-00312:位置参数关联可能不遵循命名关联” 我得到这个是因为下面一行: AttachList=> v_est_proc_name||'_E
我有以下工厂定义。 Factory.define :status do |f| end Factory.define :my_status , :parent => :status do |f|
我有 2 个具有 1:M 关联的模型,定义如下: var inventory = sequelize.define("Inventory",{**model definition**}; var tr
假设这个模式: var user = sequelize.define('user', { username: Sequelize.STRING, email: Sequelize.S
我正在使用 Apple 的工具进行应用站点关联验证,该工具位于此处:https://search.developer.apple.com/appsearch-validation-tool 它给了我错
实体\识别 /** * @ORM\Entity * @ORM\Table(name="c_rcgntn") */ class Recognition { /** * @ORM\
我是一名优秀的程序员,十分优秀!