我有一個表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對象。