nhibernate 流畅映射
我有一张桌子 http://img36.imageshack.us/i/beztytuuszxq.png/< /a> 和映射:
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 属性引用 null 或瞬态值 CMS.Domain .Entities.Article.Category
我无法将该
public virtual Category Parent { set; get; }
行更改为
public virtual Category? Parent { set; get; }
or
public virtual Nullable<Category> Parent { set; get; }
因为我在编译过程中遇到错误:
CMS.Domain.Entities.Category' 必须是不可为 null 的值类型才能将其用作泛型类型或方法“System.Nullable
我不知道要更改什么才能在没有父项的情况下保存 Category 对象。
I have a table http://img36.imageshack.us/i/beztytuuszxq.png/ and mapping:
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);
}
}
The entity looks that:
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; }
}
When I want to save an Category object to db, when Parent property is set to null, I have exception:
not-null property references a null or transient value CMS.Domain.Entities.Article.Category
I cannot change the
public virtual Category Parent { set; get; }
line to
public virtual Category? Parent { set; get; }
or
public virtual Nullable<Category> Parent { set; get; }
because I have an error during compile:
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>'
I don't know what to change to have possibility to save Category objects without parents.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能将引用类型设置为 Nullable(因为它已经是 Nullable)。
Nullable
(或T?
)只能与不可为 null 的值类型(例如int
或DateTime )。
该错误引用 CMS.Domain.Entities.Article.Category - Article 类中的 Category 属性。您尚未提供 Article 实体的映射文件,但我假设它映射 Category 属性,并且指定
Not.Nullable()
或不指定Nullable()
>。如果域模型允许文章实体包含空类别,请使用 Nullable() ,否则您需要在创建/保存文章时设置类别:
You can't make a reference type Nullable (as it already is).
Nullable<T>
(orT?
) can only be used with a non-nullable value type (such asint
orDateTime
).The error refers to CMS.Domain.Entities.Article.Category - the Category property in the Article class. You haven't provided the map file for the Article entity, however I assume it maps the Category property and either specifies
Not.Nullable()
or doesn't specificNullable()
.If the domain model allows for the Article entity to contain a null Category use
Nullable()
otherwise you need to set the category when creating/saving the Article:不能将 Category 设为 Nullable 的原因是 Nullable 仅适用于值类型,而根据定义,Category 是一种引用类型,因此已经可以支持定义为 Category 的属性上的 null 值。您能提供异常的完整堆栈跟踪吗?
The reason you can't Nullable the Category is because Nullable is only meant for value types, and Category, by definition, is a reference type and therefore already can support nulls on properties defined as Category. Can you provide a full stack trace of the exception?
我猜您正在尝试保存一篇文章(您指定了逆),因此您需要这个:
文章.类别 = 类别;
Im guessing you are trying to save an article, (you specified inverse) therefore you need this:
Article.Category = category;