NHibernate - 通过 id 或对象引用访问对象的父对象
我有以下类:
public class Parent
{
public virtual int ParentId { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
public class Child
{
public virtual int ChildId { get; set; }
public virtual string Name { get; set; }
//public virtual int ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
这些类以一对多关系映射到数据库中的相应表。在我的映射类中,我执行以下操作:
using FluentNHibernate.Mapping;
public partial class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Id(p => p.ParentId).Column.("PARENT_ID").GeneratedBy.Native();
Map(p => p.Name).Column("PARENT_NAME").Not.Nullable();
HasMany<Child>(p => p.Children).Cascade.All().LazyLoad().Inverse().AsSet();
}
}
public partial class ChildMap : ClassMap<Child>
{
public ChildMap()
{
Id(c => c.ChildId).Column("CHILD_ID").GeneratedBy.Native();
Map(c => c.Name).Column("CHILD_NAME").Not.Nullable();
References<Parent>(c => x.Parent).Column("PARENT_ID").Not.LazyLoad().Not.Nullable();
}
}
我需要做的是公开(取消注释上面的类定义中的行) Child 类中的 ParentId 属性,以便我可以执行以下操作:
var child = new Child();
child.ParentId = 1;
也就是说,我想要能够通过 child.ParentId 属性将 Parent 附加到 Child,同时仍然能够通过 child.Parent 属性访问 Parent。例如,
// i currently have to do the following in order to link the child with
// the parent when I update an existing Child instance (ParentService() and
// ChildService() are service classes that sit between my applications and
// NHibernate).
var parentService = new ParentService();
var parent = parentService.GetById(1);
var child = new Child() { ChildId = 2, Parent = parent, Name = "New Name" };
var childService = new ChildService();
childService.Save(child);
// in a different project, i access the Parent object via the child's
// Parent property
var childService = new ChildService();
var child = childService.GetById(2);
Console.WriteLine(child.Parent.Name);
// i want to do this instead
var child = new Child() { Id = 2, ParentId = 1, Name = "New Name" };
var childService = new ChildService();
childService.Save(child);
Console.WriteLine(child.Id); // 11
// [ ... ]
var childService = new ChildService();
var child = childService.GetById(2);
Console.WriteLine(child.Parent.Name);
我将如何更改映射来实现这一点? TIA,
拉尔夫·汤普森
I have the following classes:
public class Parent
{
public virtual int ParentId { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
public class Child
{
public virtual int ChildId { get; set; }
public virtual string Name { get; set; }
//public virtual int ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
These classes map to corresponding tables in the database in a one-to-many relationship. In my mapping classes, I do the following:
using FluentNHibernate.Mapping;
public partial class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Id(p => p.ParentId).Column.("PARENT_ID").GeneratedBy.Native();
Map(p => p.Name).Column("PARENT_NAME").Not.Nullable();
HasMany<Child>(p => p.Children).Cascade.All().LazyLoad().Inverse().AsSet();
}
}
public partial class ChildMap : ClassMap<Child>
{
public ChildMap()
{
Id(c => c.ChildId).Column("CHILD_ID").GeneratedBy.Native();
Map(c => c.Name).Column("CHILD_NAME").Not.Nullable();
References<Parent>(c => x.Parent).Column("PARENT_ID").Not.LazyLoad().Not.Nullable();
}
}
What I need to be able to do is expose (uncomment the line in the class definition above) the ParentId property in the Child class so that I can do the following:
var child = new Child();
child.ParentId = 1;
That is, I want to be able to attach the Parent to the Child via the child.ParentId property while still being able to access the Parent via the child.Parent property. E.g.,
// i currently have to do the following in order to link the child with
// the parent when I update an existing Child instance (ParentService() and
// ChildService() are service classes that sit between my applications and
// NHibernate).
var parentService = new ParentService();
var parent = parentService.GetById(1);
var child = new Child() { ChildId = 2, Parent = parent, Name = "New Name" };
var childService = new ChildService();
childService.Save(child);
// in a different project, i access the Parent object via the child's
// Parent property
var childService = new ChildService();
var child = childService.GetById(2);
Console.WriteLine(child.Parent.Name);
// i want to do this instead
var child = new Child() { Id = 2, ParentId = 1, Name = "New Name" };
var childService = new ChildService();
childService.Save(child);
Console.WriteLine(child.Id); // 11
// [ ... ]
var childService = new ChildService();
var child = childService.GetById(2);
Console.WriteLine(child.Parent.Name);
How would I change the mappings to make that happen? TIA,
Ralf Thompson
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是 NHibernate 的正确用法。
要获取父级的 Id,请使用:
要通过 Id 设置父级,请使用
That's not a correct usage of NHibernate.
To get the Id of the parent use:
To set the parent by Id, use