Fluent NHibernate 的一对一映射不起作用

发布于 2024-12-17 00:31:26 字数 605 浏览 3 评论 0原文

我有 2 个表(Labour 和 LabourPosition)。我不想使用 Labor 表中的 LabourPosition 表的 fki,而是使用对象 (theLabourer.LabourPosition.Name 而不是 getLabourPosition(theLabourer.LabourPositionId) 我已经阅读了几篇文章

来执行此操作,但它们似乎对我不起作用...尝试查看 LabourPosition 属性的值时遇到的错误是“无法初始化代理 - 没有会话”。

我的映射(ps:LabourPosition 没有返回到 Labour 的 fki,只有一种方式): LabourMap 类:

References<LabourPosition>(x => x.LabourPosition, "LabourPositionsId");

然后显然我已将 Labor 实体中的属性定义为:

public virtual LabourPosition LabourPosition { get; set; }

有什么想法吗?帮助将不胜感激!

I have 2 tables (Labour and LabourPosition). Instead of having a fki to the LabourPosition table from the Labour table, I'd like to use an object (theLabourer.LabourPosition.Nameinstead of getLabourPosition(theLabourer.LabourPositionId).

I've read a few articles to do this but they don't seem to be working for me... the error I get when trying to view the value of the LabourPosition property is "Could not initialize proxy - no Session".

My mapping (p.s: LabourPosition does not have a fki back to Labour, one way only):
LabourMap class:

References<LabourPosition>(x => x.LabourPosition, "LabourPositionsId");

And then obviously I've defined the property in the Labour entity as:

public virtual LabourPosition LabourPosition { get; set; }

Any ideas? Help would be appreciated!

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

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

发布评论

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

评论(2

桜花祭 2024-12-24 00:31:26

来自 流畅映射 文档

HasOne / 一对一

HasOne 通常是为特殊情况保留的。一般来说,你会使用
大多数情况下的引用关系(参见:我认为你的意思是
多对一)。如果您确实想要一对一,那么您可以使用
HasOne 方法。

HasOne(x => x.Cover);

如果您想使用引用(如代码所示),则需要映射关系的双方。从文档中:

HasMany / 一对多

HasMany 可能是最常见的基于集合的关系
你将要使用。 HasMany 是 References 的“另一面”
关系,并应用于“一侧”(一位作者有许多
图书)。现在让我们绘制一下我们开始的关系的作者方
多于。我们需要加入到 books 表中,返回一组
与该作者相关的任何书籍。

公共类作者{公共IList书籍{获取;放; } } 我们
使用AuthorMap构造函数中的HasMany方法来映射这一面
关系:

HasMany(x => x.Books);与引用一样,外键默认为
Author_id,您可以使用 KeyColumn 方法覆盖它或更改
具有约定的默认行为。

您可以使用几种不同类型的集合,并且
它们都可以在 HasMany 调用下使用。

为什么需要一对一的关系,而不是仅将所有列保留在一个表中?

From the fluent mapping documentation

HasOne / one-to-one

HasOne is usually reserved for a special case. Generally, you'd use a
References relationship in most situations (see: I think you mean a
many-to-one). If you really do want a one-to-one, then you can use the
HasOne method.

HasOne(x => x.Cover);

If you want to use a reference (as your code suggests), you need to map both sides of the relationship. From the documentation:

HasMany / one-to-many

HasMany is probably the most common collection-based relationship
you're going to use. HasMany is the "other side" of a References
relationship, and gets applied on the "one side" (one author has many
books). Now let's map the author side of the relationship we started
above. We need to join into the books table returning a collection of
any books associated with that author.

public class Author { public IList Books { get; set; } } We
use the HasMany method in the AuthorMap constructor to map this side
of the relationship:

HasMany(x => x.Books); As with References, the foreign-key defaults to
Author_id, and you can override it with the KeyColumn method or change
the default behaviour with a Convention.

There are a few different types of collections you can use, and
they're all available under the HasMany call.

Why do you need a one-one relationship, instead of just keeping all the columns in a single table?

巴黎盛开的樱花 2024-12-24 00:31:26

在你的映射中尝试

References(x => x.LabourPosition, "LabourPositionsId");

In your mapping try

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