我有一个表http://img36.imageshack.us/i/beztytuuszxq.png/和映射:的NHibernate +流畅+映射
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; }
}
当我要救一个类别对象数据库,当parent属性设置为空,我有例外:
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对象。