在将实体保存到数据库之前,如何在 NHibernate 侦听器中获取实体的 ID?
我们有一个新要求,即每个实体都必须有一个用户可分配的 ID:
public class Entity
{
public int ServerId { get; set; }
public int UserId { get; set; }
}
ServerId
是使用 HiLo 自动生成的 ID,UserId
是用户指定的 ID。但是,如果用户未设置 UserId
,则应默认为 ServerId
。所以我创建了一个像这样的监听器:
public class CustomEventListener : IPreInsertEventListener
{
public bool OnPreInsert(PreInsertEvent @event)
{
var entity = @event.Entity as Entity;
if (entity.UserId == 0)
{
entity.UserId = entity.ServerId;
}
return false;
}
}
不幸的是,在这个预插入阶段,ServerId
始终为0,因此UserId
始终为0。有谁知道我该怎么做获取生成的ServerId
在保存到数据库之前但之后由NHibernate填充ServerId
?
PS 我还假设这对于 Identity
生成器来说是不可能的,因为它保存到数据库,然后获取数据库使用的 ID。有人可以证实这一点吗?
We have a new requirement that every entity must have a user-assignable ID:
public class Entity
{
public int ServerId { get; set; }
public int UserId { get; set; }
}
The ServerId
is an auto-generated ID using HiLo and the UserId
is the user-specified ID. However, if the user does NOT set a UserId
, it should default to whatever ServerId
is. So I created a listener like this:
public class CustomEventListener : IPreInsertEventListener
{
public bool OnPreInsert(PreInsertEvent @event)
{
var entity = @event.Entity as Entity;
if (entity.UserId == 0)
{
entity.UserId = entity.ServerId;
}
return false;
}
}
Unfortunately, at this pre-insert stage, the ServerId
is always 0, so the UserId
is always 0. Does anyone know how I can get the generated ServerId
before it's saved to the database but after the ServerId
is populated by NHibernate?
P.S. I'm also assuming that this is impossible with the Identity
generator because it saves to the database and then gets the ID that the DB used. Can someone confirm this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 Ayende 的博客文章在这里找到了解决方案:
http ://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx
结果你不仅要在实体中更改它,还要更改实体状态:
Found the solution thanks to Ayende's blog post here:
http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx
Turns out you have to not only change it in the entity, but also the entity state: