如何使用 Fluent NHibernate 配置以下连接?
这是基于遗留系统的。
我有以下表格:
CREATE TABLE a
id int
CREATE TABLE b
a_id int,
c_id int
relationshipid int -- must be IN (1, 2, 3)
CREATE TABLE c
id int
我想要以下域模型
public class A
{
public int Id { get; set; }
public C entityc { get ; set; }
}
public class C
{
public int Id { get; set; }
}
表 b 的设置使得对于特定定义的关系 ID 来说,有(嗯,应该只有)一对 id。对于其他关系,通过 B 的一对一映射并不成立。 Relationshipid 可以是少数值之一。
如何使用流畅的NHIbernate将实体C从relationshipid为1的关系中获取到类A?
作为一个附带问题,我在这里尝试做的事情有一个名称吗?最初的方法是尝试使用 HasOne 和 Join 表并过滤结果,但显然失败了。
编辑:澄清关系 ID 和目的。
This is based on a legacy system.
I have the following tables:
CREATE TABLE a
id int
CREATE TABLE b
a_id int,
c_id int
relationshipid int -- must be IN (1, 2, 3)
CREATE TABLE c
id int
I want the following domain models
public class A
{
public int Id { get; set; }
public C entityc { get ; set; }
}
public class C
{
public int Id { get; set; }
}
Table b is set up so that for a particular defined relationshipid there is (well, should only be) one pair of ids. For other relationships, that one to one mapping through B doesn't hold true. Relationshipid can be one of a small number of values.
How do I get entity C into class A from the relationship where the relationshipid is 1 using fluent NHIbernate?
As a side question, is there a name for what I am trying to do here? The original approach was trying use a HasOne with a Join table and Filter the results, but obviously that failed miserably.
EDIT: Clarified RelationshipID and purpose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为映射此问题的最简单方法是使表 b 成为一个实体,并引用该实体中的 A 和 C,并以 RelationshipId 作为 id。因此,您的映射将如下所示:
编辑:
如果您想将 A 实体中的 B 实体集合的结果过滤为仅匹配 RelationshipId = 1 的结果,那么您应该看看这个帖子:
流畅的 NHibernate 和过滤一-查询上的多对多关系需要多个连接?
您也可以在 A 类中执行类似的操作:
I think the easiest way to map this would be to make your table b an entity and have references to both A and C within that entity and RelationshipId as the id. So your mappings would look something like this:
Edit:
If your wanting to filter these results for the collection of B entities in your A entity to only ones matching RelationshipId = 1 then you should take a look at this post:
Fluent NHibernate and filtering one-to-many relationship on query requiring multiple joins?
You could also do something like this in your class A: