Castle Active Record NHibernate Same Session 同一对象的2个版本
我在数据库中有一辆汽车,其中模型字段是“法拉利”,
using (new TransactionScope())
{
var car = Find(1);
car.Model = "Ferrari Plus";
// i need the old Car value to make a comparison
var car2 = Find(1);
// i need here the db Car record, instead i have the cached Nhibernate value
if (car2.Model == 'Ferrari")
// do something
}
我以这种方式修改了代码,
using (new TransactionScope())
{
var car = Find(1);
car.Model = "Ferrari Plus";
// i need the old Car value to make a comparison
using (new TransactionScope())
{
var car2 = Find(1);
// i got the db value buuuuuuut
// if i save because i need to save the modific Car i got an error
if (car2.Model == 'Ferrari")
car.Save(); // ERROR: 2 objects with the same id exists
}
}
如何避免这个问题? 如何将同一对象的 2 个版本放入同一个 NHibernate 会话中?
I have a Car in DB where Model field is "Ferrari"
using (new TransactionScope())
{
var car = Find(1);
car.Model = "Ferrari Plus";
// i need the old Car value to make a comparison
var car2 = Find(1);
// i need here the db Car record, instead i have the cached Nhibernate value
if (car2.Model == 'Ferrari")
// do something
}
i modified the code in this way
using (new TransactionScope())
{
var car = Find(1);
car.Model = "Ferrari Plus";
// i need the old Car value to make a comparison
using (new TransactionScope())
{
var car2 = Find(1);
// i got the db value buuuuuuut
// if i save because i need to save the modific Car i got an error
if (car2.Model == 'Ferrari")
car.Save(); // ERROR: 2 objects with the same id exists
}
}
how can i avoid this problem?
How can i get 2 versions of the same object into the same NHibernate Session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
nhibernate 的基本原则之一是在同一个会话中不要有 2 个相同的对象。据我所知,你所要求的事情无法完成。
可能的解决方案 1
您可以在一个会话中查询
car
关闭会话。在另一个会话中查询car2
并与两个对象进行比较。使用car
的属性更新car2
。可能的解决方案 2
执行与上述相同的操作,但不要将值从
car
复制到car2
。相反,您可以打开另一个会话并执行session.Update(car);
可能的解决方案 3
您可以做的是创建原始会话的副本或创建 dto。当您进行比较时,将持久实例(更改的副本)与副本/dto 进行比较。
One of the base principles of nhibernate is to not have 2 objects that are the same in the same session. What you are asking cannot be done as far as I know.
Possible Solution 1
You could query
car
in one session close the session. Querycar2
in another session and do your compare against the two objects. Updatecar2
withcar
's properties.Possible Solution 2
Do the same as above except don't copy the values from
car
tocar2
. Instead you would open another session and do asession.Update(car);
Possible Solution 3
You could do is to create a copy of the original or create a dto. When you do your comparison compare the persistent instance (changed copy) to the copy/dto.
您可以 Evict() 一个对象并告诉 ActiveRecord / NHibernate 不再跟踪该对象。但您需要在开始更改之前执行此操作。所以你的代码应该是这样的:
看看这个 Active Record 生命周期图。
You can Evict() an object and tell ActiveRecord / NHibernate to no longer track this Object. But you need to do this before you start changing it. So your code should look like this:
Take a Look at this Active Record Lifecycle Diagram.