nhibernate 流畅映射

发布于 2024-09-24 02:47:44 字数 1542 浏览 2 评论 0原文

我有一张桌子 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”中的参数“T”

我不知道要更改什么才能在没有父项的情况下保存 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吲‖鸣 2024-10-01 02:47:44

您不能将引用类型设置为 Nullable(因为它已经是 Nullable)。 Nullable(或 T?)只能与不可为 null 的值类型(例如 intDateTime )。

该错误引用 CMS.Domain.Entities.Article.Category - Article 类中的 Category 属性。您尚未提供 Article 实体的映射文件,但我假设它映射 Category 属性,并且指定 Not.Nullable() 或不指定 Nullable() >。

如果域模型允许文章实体包含空类别,请使用 Nullable() ,否则您需要在创建/保存文章时设置类别:

Article.Category = aCategory;

You can't make a reference type Nullable (as it already is). Nullable<T> (or T?) can only be used with a non-nullable value type (such as int or DateTime).

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 specific Nullable().

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:

Article.Category = aCategory;
最偏执的依靠 2024-10-01 02:47:44

不能将 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?

花想c 2024-10-01 02:47:44

我猜您正在尝试保存一篇文章(您指定了逆),因此您需要这个:
文章.类别 = 类别;

Im guessing you are trying to save an article, (you specified inverse) therefore you need this:
Article.Category = category;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文