我必须加载/ NHibernate的SaveOrUpdate之前获取的实体? [英] Do I have to Load/Get an entity before SaveOrUpdate in Nhibernate?
问题描述
在我的ASP.NET MVC应用程序,我从分开视图模型域模型。结果
我改变我的实体在一个视图模型对象,这样我就可以养活我的看法,只有需要的数据(我用于此目的valueinjecter)。结果
在保存过程我的控制器回来的视图模型对象时,它变成一个域模型实体,并尝试用SaveOrUpdate坚持它。
我发现,如果我试图更新现有记录,NHibernate的认为它作为一个新的对象并强制执行INSERT,即使我的实体有一个正确的ID。结果
我没装(获取/加载)原因之前实体我想避免再次重新映射所有字段。结果
有什么我做错了吗?什么是实现这一目标的最佳途径?
in my ASP.NET MVC application I've separated the domain model from the view model.
I transform my entity in a viewmodel object so I can "feed" my views with only the data needed (I used valueinjecter for this purpose).
During the save process my controller gets back the viewmodel object, transforms it into a domain model entity and try to persist it with SaveOrUpdate.
I've noticed that if I try to update an existing record, Nhibernate considers it as a new object and forces an INSERT, even if my entity has a proper ID.
I haven't loaded (Get/Load) the entity before cause I would like to avoid to re-map all the fields again.
Is there anything I am doing wrong? What's the best way to achieve that?
的* - 更新 - *
* - UPDATE - *
我的控制器接收用户(视图模型),验证它并试图挽救它通过服务层作为一个实体:
My controller receives a User (ViewModel), validates it and tries to save it through a service layer as an entity:
public ActionResult Edit(Guid id, Models.User User)
{
...
var user = new Domain.User();
user.InjectFrom(User);
user.SetId(id);
user = this._SecurityService.SaveUser(user);
}
这是该服务:
public Domain.User SaveUser(Domain.User User)
{
bool Result = false;
if (this._ValidationEngine.IsValid(User))
{
using (_UnitOfWork)
{
if (User.Code != Guid.Empty)
{
var user = this._UserRepository.Load(User.Code);
user.InjectFrom(User);
User = this._UserRepository.Update(user);
}
else {
User = this._UserRepository.Save(User);
}
Result = _UnitOfWork.Commit();
}
}
return (User);
}
我已经有了一个大概的事实,我不得不改变我的ViewModel /实体这么多次的关注。现在,当我尝试保存新的用户,我得到这个消息:行被其它事务更新或删除(或者未保存价值的映射是不正确的)结果
也许这是关系到什么达林告诉我一些方法。结果
是否有更好的方法做什么,我试图做?
I've got a concern about the fact that I have to transform my viewmodel/entity so many times. Now when I try to save a new User I got this message: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
Probably this is in some ways related to what Darin was telling me.
Are there any better ways to do what I am trying to do?
更新
我注意到,错误的行被更新或者删除了......是由事实,我有我的映射定义的版本引起的。我可以理解的是我需要在标识和版本匹配我想更新的实体。结果
我想了解其他人如何与DDD + ASP.NET MVC + NHibernate的???
I've noticed that the error "Row was updated or deleted..." is caused by the fact that I have a version defined for my mappings. What I can understand is I need to have the Id and the version matching the entity I want to update.
I would like to understand how other people do these things with DDD + ASP.NET MVC + NHibernate ???
SOLUTION
我所有的实体版本定义(我忘了说了,不好意思):
All my entities have a version defined (I forgot to say, sorry):
<version name="Version" type="System.Int32" unsaved-value="0">
<column name="Version" not-null="true" />
</version>
随着Ayende解释这里 ,NHibernate的尝试更新我的实体像这样的查询:
As Ayende explained here, Nhibernate tries to update my entity with a query like this:
UPDATE Table SET field1 = 'bla', Version = (y+1) WHERE id = x AND Version = y
其中y应该是我的实体的版本。由于我没有填充我的实体版本,它会产生异常StaleObjectException异常。结果
我添加了一个版本字段到我的视图模型。我把它保存在我的ID视图一起隐藏字段。结果
我现在控制器接收到一个ID和一个版本视图模型。
我在域实体注入这些领域,让库保存它(我不重装我的实体):
where y should be the version of my entity. Since I wasn't populating my entity with a version, it would generate an exception StaleObjectException.
I've added a version field to my viewmodel. I save it in a hidden field of my view together with the id.
My controller now receives a viewmodel with an Id and a version.
I inject these fields in my domain entity and let the repository save it (I don't reload my entity):
User = this._UserRepository.Update(user);
如果我得到一个 StaleObjectException异常的异常就意味着别人已经更新了数据库行,我会提供一些反馈给用户。
If I get a StaleObjectException exception it means that someone else has updated the DB row and I'll provide the user with some feedback.
推荐答案
既然你已经有一个ID,因此,知道这是一个更新,请使用 session.Update
而不是 SaveOrUpdate
。
Since you already have an Id and, therefore, know this is an update, use session.Update
instead of SaveOrUpdate
.
这篇关于我必须加载/ NHibernate的SaveOrUpdate之前获取的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!