简体   繁体   English

流利的NHibernate一对一,只有一个外部色谱柱

[英]Fluent NHibernate One-To-One with only one foreign column

I have 2 one-to-one tables in my db, and only one of them has foreign key to another table. 我的数据库中有2个一对一表,并且只有其中一个表具有到另一个表的外键。 Here is an example of what I have 这是我所拥有的一个例子

Table 1: Container 表1: 容器
- Id (PK) -ID(PK)

Table 2: Item 表2: 项目
- Id (PK) -ID(PK)
- ContainerId (FK) -ContainerId(FK)

Can I configure their relations as one to one to have following models mapped: 我可以一对一地配置它们的关系以映射以下模型:

public class Container{
     public virtual int Id {get;set;}
     public virtual Item Item {get;set;}
}

public class Item{
     public virtual int Id {get;set;}
     public virtual Container {get;set;}
}

I can configure Item to have one Container, because it has explicit column for this, but how it can be done for Container ? 我可以将Item配置为具有一个Container,因为它对此具有显式列,但是如何为Container完成它呢?

Thanks! 谢谢!

That is not a true one to one relationship. 那不是真正的一对一关系。 Both primary keys must be the same in both tables for it to be a one to one. 两个主键在两个表中必须相同,才能一对一。 Container.Id != Item.Id. Container.Id!=Item.Id。 That is a many-to-one . 那是many-to-one

In your item mapping you would have something like this: 在项目映射中,您将具有以下内容:

Id(x => x.Id);
References(x => x.Container);

Your Container mapping would be similar. 您的Container映射将类似。

http://www.jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/ http://www.jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/

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

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