Fluent nHIbernate - 同一个表中的 HasMany 关系
我正在为网站顶部菜单结构创建一个模型 -
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论如何,你必须将它“链接”到它的父级,如果不想得到异常......
仅当您未在映射中指定
Cascade.Save\All()
时,您才必须独立保存子级:您必须添加一个
Parent
属性来连接“子”到“父”。它的映射是:
PS
您不必在
Id
上编写Not.Nullable
,这是默认值。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:You have to add a
Parent
property to connect the "child" to it's "Parent".It's mapping is:
P.S.
You don't have to write
Not.Nullable
onId
, It's the defaults.