【发布时间】:2010-09-21 02:06:59
【问题描述】:
我有一张桌子http://img36.imageshack.us/i/beztytuuszxq.png/ 和映射:
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table(FieldNames.Category.Table);
Id(x => x.ID);
Map(x => x.Name).Not.Nullable();
Map(x => x.ShowInMenuBar).Not.Nullable();
References(x => x.Parent).Column(FieldNames.Category.ID).Nullable();
HasMany(x => x.Articles).Cascade.All().Inverse().Table(FieldNames.Article.Table);
}
}
实体看起来:
public class Category : EntityBase
{
public virtual int ID { set; get; }
public virtual string Name { set; get; }
public virtual Category Parent { set; get; }
public virtual bool ShowInMenuBar { set; get; }
public virtual IList<Article> Articles { set; get; }
}
当我想将 Category 对象保存到 db 时,当 Parent 属性设置为 null 时,出现异常:
not-null property references a null or transient value CMS.Domain.Entities.Article.Category
我无法更改
public virtual Category Parent { set; get; }
行到
public virtual Category? Parent { set; get; }
或
public virtual Nullable<Category> Parent { set; get; }
因为我在编译的时候出错了:
CMS.Domain.Entities.Category' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
我不知道要更改什么才能在没有父母的情况下保存 Category 对象。
【问题讨论】:
标签: nhibernate fluent-nhibernate nhibernate-mapping