开发者

How to reliably determine that an entity is transient?

With NHibernate, how does one reliably check that a given entity is transient (i.e. hasn't been persisted yet)? The resources I have seen recommend something similar to this:

public bool IsTransient()
{
  开发者_StackOverflow中文版  return this.Id == default(Guid);
}

Assuming my already persisted entity has an integer ID and it is somehow equal to 0, wouldn't this method fail?


If 0 is a valid primary key in your context, then yes, that would be unreliable.

Basically, the "unsaved-value" on the id of the object determines if it's transient or persistent. By default, it's set to null or default() for the type. You can choose to set this manually when you do your mapping.

As long as your logic in the code above conforms to what Nhibernate believes is a transient object, you're good. And Nhibernate will take any object whose Id property equals that of "unsaved-value" to be transient.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜