如何使用 Fluent NHibernate 配置以下连接?

发布于 2024-11-01 07:08:49 字数 621 浏览 1 评论 0原文

这是基于遗留系统的。

我有以下表格:

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 技术交流群。

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

发布评论

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

评论(1

嗳卜坏 2024-11-08 07:08:49

我认为映射此问题的最简单方法是使表 b 成为一个实体,并引用该实体中的 A 和 C,并以 RelationshipId 作为 id。因此,您的映射将如下所示:

public class A
{
    public int Id { get; set; }

    public IList<B> bEntities { get; set; }
}

public class ClassAMap : ClassMap<A>
{
    public AMap()
    {
        Table("A");
        Id(x => x.Id);

        HasMany(x => x.bEntities)
            .KeyColumns.Add("a_id");
    }
}

public class B
{
    public virtual int RelationshipId { get; set; }
    public virtual A InstanceA { get; set; }
    public virtual C InstanceC { get; set; }
}

public class ClassBMap : ClassMap<B>
{
    public BMap()
    {
        Table("B");
        Id(x => x.RelationshipId , "relationshipid");

        References(x => x.InstanceA);
        References(x => x.InstanceC);
    }
}  

编辑:

如果您想将 A 实体中的 B 实体集合的结果过滤为仅匹配 RelationshipId = 1 的结果,那么您应该看看这个帖子:

流畅的 NHibernate 和过滤一-查询上的多对多关系需要多个连接?

您也可以在 A 类中执行类似的操作:

public class A
{
    public int Id { get; set; }

    public IList<B> bEntities { get; set; }

    public C InstanceC
    {
       get { return bEntities.First<B>(x => x.RelationshipId == 1).InstanceC; }
    }
}

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:

public class A
{
    public int Id { get; set; }

    public IList<B> bEntities { get; set; }
}

public class ClassAMap : ClassMap<A>
{
    public AMap()
    {
        Table("A");
        Id(x => x.Id);

        HasMany(x => x.bEntities)
            .KeyColumns.Add("a_id");
    }
}

public class B
{
    public virtual int RelationshipId { get; set; }
    public virtual A InstanceA { get; set; }
    public virtual C InstanceC { get; set; }
}

public class ClassBMap : ClassMap<B>
{
    public BMap()
    {
        Table("B");
        Id(x => x.RelationshipId , "relationshipid");

        References(x => x.InstanceA);
        References(x => x.InstanceC);
    }
}  

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:

public class A
{
    public int Id { get; set; }

    public IList<B> bEntities { get; set; }

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