NHibernate - 通过 id 或对象引用访问对象的父对象

发布于 2024-10-25 03:56:40 字数 2490 浏览 1 评论 0原文

我有以下类:

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 技术交流群。

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

发布评论

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

评论(1

情绪少女 2024-11-01 03:56:40

这不是 NHibernate 的正确用法。

要获取父级的 Id,请使用:

var parentId = child.Parent.Id; //this does not cause loading

要通过 Id 设置父级,请使用

child.Parent = session.Load<Parent>(parentId); //this never goes to the DB either

That's not a correct usage of NHibernate.

To get the Id of the parent use:

var parentId = child.Parent.Id; //this does not cause loading

To set the parent by Id, use

child.Parent = session.Load<Parent>(parentId); //this never goes to the DB either
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文