开发者

Cannot set entity properties in NHibernate event listener

开发者 https://www.devze.com 2023-01-06 16:26 出处:网络
I\'m overriding NHibernate\'s PreInsertEventListener in order to set the entity\'s DateCreated property t开发者_如何学Co DateTime.Now.

I'm overriding NHibernate's PreInsertEventListener in order to set the entity's DateCreated property t开发者_如何学Co DateTime.Now.

Here is my code:

public bool OnPreInsert(PreInsertEvent e)
{
  DomainObject domainObject = (DomainObject) e.Entity;
  if (domainObject.CreatedById == 0) throw new Exception("The " + domainObject.GetType().Name + " cannot be created if its CreatedById property has not been set.");
  domainObject.DateCreated = DateTime.Now;
  return false;
}

I am finding that any entity properties set here (for example, the call to DateCreated above) do not find their way into the update SQL created by NHibernate. Does anyone know what gives?

Yes, I have cofirmed that my event listener is being called!

Thanks

David


Hmmm, it seems you have to use a specific syntax to modify the entity's properties at this stage of the game.

That syntax is demonstrated here:

Why both NHibernate OnPreInsert and OnPreUpdate methods get called for an object

Note that I've discovered you don't also have to set the entity's properties the normal way as well, which this code does.

Thanks

David

0

精彩评论

暂无评论...
验证码 换一张
取 消