NHibernate 映射 - 通过表进行多对一

发布于 2024-09-16 20:46:34 字数 673 浏览 3 评论 0原文

我正在使用具有以下结构的现有数据库。更改数据库架构是最后的手段。

Products
   Id
   Name

ParentProducts
   ParentId
   ChildId

我不需要 ParentProducts 的实体,我有以下子属性(仍然需要测试它,但这就是概念)。

<bag name="Children" lazy="true" table="dbo.ParentProducts" cascade="save-update" inverse="true" >
   <key column="[ChildId]"></key>
   <many-to-many column="[ProductId]" class="Product" />
</bag>

我所困扰的是如何创建父属性?我想做类似以下的事情,但 table 不是多对一的有效属性。

<many-to-one name="Parent" column="[ParentId]" table="dbo.ParentRelated" class="Policy" />

我可以创建一个包,然后只查看第一个项目,但这更像是一种黑客行为。

有什么想法吗? 谢谢

I'm working with an existing database that has the following structure. Changing the database schema is a last resort.

Products
   Id
   Name

ParentProducts
   ParentId
   ChildId

I don't want an entity for ParentProducts, I have the following for the children property (still need to test it, but that's the concept).

<bag name="Children" lazy="true" table="dbo.ParentProducts" cascade="save-update" inverse="true" >
   <key column="[ChildId]"></key>
   <many-to-many column="[ProductId]" class="Product" />
</bag>

What I'm struggling with is how do I create a Parent property? I'd like to do something like the following, but table isn't a valid attribute for many-to-one.

<many-to-one name="Parent" column="[ParentId]" table="dbo.ParentRelated" class="Policy" />

I could create a bag and only ever look at the first item, but that's more of a hack.

Any ideas?
Thanks

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

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

发布评论

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

评论(1

我的奇迹 2024-09-23 20:46:34

创建一个袋子是最简单的解决方案。您可以给它一个干净的接口:

protected virtual ICollection<Product> Parents { get; set; }

public virtual Product Parent
{
    get
    {
        return Parents.SingleOrDefault();
    }
    set
    {
        Parents.Clear();
        Parents.Add(value);
    }
}

这样,其余代码就不需要了解数据库/映射结构。

Creating a bag is the easiest solution. And you can give it a clean interface:

protected virtual ICollection<Product> Parents { get; set; }

public virtual Product Parent
{
    get
    {
        return Parents.SingleOrDefault();
    }
    set
    {
        Parents.Clear();
        Parents.Add(value);
    }
}

With this, the rest of the code doesn't need to be aware of the DB/mapping structure.

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