在 FluentNHibernate 中指定 HasMany() 关系映射上的表

发布于 2024-10-29 01:51:29 字数 635 浏览 1 评论 0原文

我在 FluentNHibernate 中有一个 HasMany 关系的映射,我想在其上指定一个表来覆盖 nHibernate 将查找的默认表以查找我拥有的许多对象。这有道理吗?

假设我有一个发票表和一个 InvoiceItems 表,并且假设我有一个名为 InvoiceItemsTwo 的表。

我有一个 Invoice 类和一个 InvoiceItems 类,它们的映射非常简单。我想在 Invoice 的映射中指定它应该在 InvoiceItemsTwo 而不是默认的 InvoiceItems 中查找它的项目。

所以我对这种关系的映射看起来像这样

HasMany(c => c.InvoiceItems).Cascade.SaveUpdate().Table("InvoiceItemsTwo");

但这不起作用。我在运行时不断从我的网站收到错误,提示对象名称“InvoiceItems”无效。

为什么它忽略了我在关系映射中显式指定表的事实?

我尝试在运行时转储映射,并且正在设置类似这样的内容

<bag cascade="save-update" table="InvoiceItemsTwo">

有什么想法吗?

I have a mapping in FluentNHibernate for a HasMany relationship and I'd like to specify a Table on it to override the default Table that nHibernate will look in to find those objects that I have many of. Does that make sense?

So lets say I have a table for Invoices and a table for InvoiceItems and lets say I have table called InvoiceItemsTwo.

I have a class for Invoice and a Class for InvoiceItems as well, and their mappings are pretty straight forward. I'd like to specify in my mapping for Invoice, that it should look for it's items in InvoiceItemsTwo instead of the default InvoiceItems.

So my mapping of that relationship looks like this

HasMany(c => c.InvoiceItems).Cascade.SaveUpdate().Table("InvoiceItemsTwo");

But this doesn't work. I keep getting an error from my website at runtime that says Invalid object name 'InvoiceItems'.

Why is it ignoring the fact that I am explicitly specifying the Table in my mapping on the relationship?

I tried dumping the mapping at run time and it's being setup something like this

<bag cascade="save-update" table="InvoiceItemsTwo">

Any ideas?

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

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

发布评论

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

评论(3

七月上 2024-11-05 01:51:29

table 属性仅适用于多对多关系,不适用于一对多关系。

The table attribute applies only to many-to-many relationships, not one-to-many.

以为你会在 2024-11-05 01:51:29

您不能在映射类中指定不同的表。 Fluent NHibernate 使用映射到属性列表 (InvoiceItems) 上的类。
如果您想使用另一个类来映射您的详细信息表,您必须创建一个 InvoceItemsTwo 类并将其映射到您的主表类中。

you can't specify a different table in your mapping class. Fluent NHibernate uses the class mapped on the property list (InvoiceItems).
If yoy want to use another class to map your details table you must create a InvoceItemsTwo class and map it in your master table class.

听闻余生 2024-11-05 01:51:29

您可以将列表映射为复合元素而不是一对多关系,然后将其映射到另一个表。但这不是一个好主意。考虑一下 NH 需要知道在内存中存储对象的位置。所以可能会出现对象存储在错误的表中的情况。

  • 使用复合元素而不是一对多和组件而不是多对一将所有 InvoiceItems 存储在单独的表中(但这在 Fluent 中称为)。
  • 或者将所有 InvoiceItems 存储在同一个表中并使用常规引用。

You could map the list as composite-element instead of a one-to-many relation and then map it to another table. But it is not a good idea. Consider that NH needs to know where to store an object which is in memory. So it may happen that the object is stored in the wrong table.

  • Either store all the InvoiceItems in separate tables using composite-element instead of one-to-many and components instead of many-to-one (however this is called in Fluent).
  • Or store all the InvoiceItems in the same table and use regular references.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文