Fluent nHIbernate - 同一个表中的 HasMany 关系

发布于 2024-12-28 09:32:57 字数 1457 浏览 1 评论 0原文

我正在为网站顶部菜单结构创建一个模型 -

我有一个 MenuObject 模型:

public class MenuObject
{
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual List<MenuObject> Children { get; set; }
}

和一个映射:

public mapMenu()
{
    Id(x => x.Id)
        .Not.Nullable();
    Map(x => x.Title)
        .Not.Nullable();
    HasMany<MenuObject>(x => x.Children)
         .AsList();
}

基本上我希望能够创建一个“顶级”菜单项,然后向其中添加一些子项 - 用数据库术语,应该有一个 ParentId 字段,其中包含父菜单项的 ID(如果有的话 - 这可能为空)

我正在努力弄清楚如何在我的对象模型中定义它。另外,一旦我配置了这个,我将如何拯救孩子?是否会类似于

public void InsertChild(MenuObject parent, MenuObject child)
{
    parent.Children.add(child)
    session.SAve(parent)
    .....
}

或者我是否必须独立保存孩子并将其显式链接到父母?

编辑*****

谢谢 - 所以现在我的模型中有以下内容:

        public virtual MenuObject Parent { get; set; }
        public virtual List<MenuObject> Children { get; set; }

并且在映射中:

 HasMany(x => x.Children)
       .AsList()
       .Inverse()
       .Cascade.All()
       .KeyColumn("ParentId");

        References(x => x.Parent, "ParentId");

我现在可以添加子项通过以下方式对父母说:

oChild.Parent = oParent;
session.SaveOrUpdate(oParent);
session.Save(oChild);
transaction.Commit();

我想我是胜利者!这是最好的方法吗?谢谢格多伦

I'm creating a model for a website top-menu structure -

I have a MenuObject model:

public class MenuObject
{
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual List<MenuObject> Children { get; set; }
}

and a mapping:

public mapMenu()
{
    Id(x => x.Id)
        .Not.Nullable();
    Map(x => x.Title)
        .Not.Nullable();
    HasMany<MenuObject>(x => x.Children)
         .AsList();
}

Basically i want to be able to create a "Top Level" Menu item then add some child items to it - in database terms, there should be a ParentId field that contains the ID of the parent Menu Item (if any - this could be null)

I'm struggling to get my head around how this should be defined in my object model. Also once i hav this configured, how would i go about saving children? Would it be something like

public void InsertChild(MenuObject parent, MenuObject child)
{
    parent.Children.add(child)
    session.SAve(parent)
    .....
}

or would I have to save the child independantly and link it to the parent explicitly?

Edit *****

Thanks - so now i have the following in my model:

        public virtual MenuObject Parent { get; set; }
        public virtual List<MenuObject> Children { get; set; }

and this in the mapping:

 HasMany(x => x.Children)
       .AsList()
       .Inverse()
       .Cascade.All()
       .KeyColumn("ParentId");

        References(x => x.Parent, "ParentId");

I can now add children to parent itms in the following way:

oChild.Parent = oParent;
session.SaveOrUpdate(oParent);
session.Save(oChild);
transaction.Commit();

I think i'm onto a winner! Is that the best way to do it? THanks gdoron

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

娇女薄笑 2025-01-04 09:32:57

或者我是否必须独立保存子项并将其显式链接到父项?

无论如何,你必须将它“链接”到它的父级,如果不想得到异常......
仅当您未在映射中指定 Cascade.Save\All() 时,您才必须独立保存子级:

HasMany<MenuObject>(x => x.Children)
    .AsList().Inverse.Cascade.All(); // Inverse will increase performance.

您必须添加一个 Parent 属性来连接“子”到“父”。
它的映射是:

References(x => x.Parent);       

PS
您不必在 Id 上编写 Not.Nullable,这是默认值。

or would i have to save the child independantly and link it to the parent explicitly?

Anyway you have to "link' it to it's parent, If don't want to get exceptions...
You will have to save the child independently only if you don't specify Cascade.Save\All() in the mapping:

HasMany<MenuObject>(x => x.Children)
    .AsList().Inverse.Cascade.All(); // Inverse will increase performance.

You have to add a Parent property to connect the "child" to it's "Parent".
It's mapping is:

References(x => x.Parent);       

P.S.
You don't have to write Not.Nullable on Id, It's the defaults.

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