Nhibernate stores id=0 as null

和自甴很熟 提交于 2019-12-04 14:03:35

The issue here would be the unsaved-value. NHibernate must decide if operations with object will be insert or update. This decision comes from unsaved-value setting, which is by default for int set to 0.

Try to extend your mapping of a ChildClass:

public ChildClassMap()
{
   Table("Metody");
   Id(x => x.Id)
       .Column("Id")
       .UnsavedValue(-1);
   ...

See 5.1.4. id, cite:

unsaved-value (optional - defaults to a "sensible" value): An identifier property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from transient instances that were saved or loaded in a previous session.

And here is nice Id mapping overview by Adam Bar (the second half of the article)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!