简体   繁体   English

使用Fluent NHibernate进行一对一映射无法正常工作

[英]One-to-one mapping with Fluent NHibernate not working

I have 2 tables (Labour and LabourPosition). 我有2个表(Labor和LabourPosition)。 Instead of having a fki to the LabourPosition table from the Labour table, I'd like to use an object (theLabourer.LabourPosition.Name instead of getLabourPosition(theLabourer.LabourPositionId) . 我想使用一个对象(theLabourer.LabourPosition.Name而不是getLabourPosition(theLabourer.LabourPositionId)而不是从Labor表中获取到LabourPosition表的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". 我已经阅读了一些文章来做到这一点,但它们似乎并不适合我......我在尝试查看LabourPosition属性的值时得到的错误是“无法初始化代理 - 没有会话”。

My mapping (ps: LabourPosition does not have a fki back to Labour, one way only): LabourMap class: 我的映射(ps:LabourPosition没有fki回到Labor,只有一种方式):LabourMap类:

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

And then obviously I've defined the property in the Labour entity as: 然后显然我已经将Labor实体中的属性定义为:

public virtual LabourPosition LabourPosition { get; set; }

Any ideas? 有任何想法吗? Help would be appreciated! 帮助将不胜感激!

From the fluent mapping documentation 流畅的映射文档

HasOne / one-to-one HasOne /一对一

HasOne is usually reserved for a special case. HasOne通常用于特殊情况。 Generally, you'd use a References relationship in most situations (see: I think you mean a many-to-one). 通常,在大多数情况下你会使用References关系(参见:我认为你的意思是多对一)。 If you really do want a one-to-one, then you can use the HasOne method. 如果你真的想要一对一,那么你可以使用HasOne方法。

HasOne(x => x.Cover); 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 /一对多

HasMany is probably the most common collection-based relationship you're going to use. HasMany可能是您将要使用的最常见的基于集合的关系。 HasMany is the "other side" of a References relationship, and gets applied on the "one side" (one author has many books). HasMany是References关系的“另一面”,并且被应用于“一面”(一位作者有很多书)。 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; 公共课作者{public IList Books {get; set; 组; } } We use the HasMany method in the AuthorMap constructor to map this side of the relationship: 我们使用AuthorMap构造函数中的HasMany方法来映射关系的这一面:

HasMany(x => x.Books); 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. 与引用一样,外键默认为Author_id,您可以使用KeyColumn方法覆盖它,或使用约定更改默认行为。

There are a few different types of collections you can use, and they're all available under the HasMany call. 您可以使用几种不同类型的集合,并且它们都可以在HasMany调用下使用。

Why do you need a one-one relationship, instead of just keeping all the columns in a single table? 为什么你需要一对一的关系,而不是只保留一个表中的所有列?

在你的映射中试试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM