FluentNHibernate,从另一个表获取 1 列

发布于 2024-09-03 17:17:48 字数 1310 浏览 2 评论 0原文

我们正在使用 FluentNHibernate,并且遇到了一个问题,我们的对象模型需要来自两个表的数据,如下所示:

public class MyModel
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
   public virtual int FooId { get; set; }
   public virtual string FooName { get; set; }
}

其中有一个 MyModel 表,其中 Id、Name 和 FooId 作为 Foo 表的外键。 Foo 表包含 Id 和 FooName。

这个问题与这里的另一篇文章非常相似: Nhibernate:连接表并从其他表获取单列,但我试图弄清楚如何使用 FluentNHibernate 来做到这一点。

我可以很容易地创建 Id、Name 和 FooId。但是映射 FooName 时我遇到了麻烦。这是我的类映射:

public class MyModelClassMap : ClassMap<MyModel>
{
   public MyModelClassMap()
   {
      this.Id(a => a.Id).Column("AccountId").GeneratedBy.Identity();
      this.Map(a => a.Name);
      this.Map(a => a.FooId);

      // my attempt to map FooName but it doesn't work
      this.Join("Foo", join => join.KeyColumn("FooId").Map(a => a.FooName));
   }
}

使用该映射,我收到此错误:

名称空间“urn:nhibernate-mapping-2.2”中的元素“class”在名称空间“urn:nhibernate-mapping-2.2”中具有无效的子元素“join”。预期可能元素的列表:命名空间“urn:nhibernate-mapping-2.2”中的“joined-subclass、loader、sql-insert、sql-update、sql-delete、filter、resultset、query、sql-query”。

有什么想法吗?

We're using FluentNHibernate and we have run into a problem where our object model requires data from two tables like so:

public class MyModel
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
   public virtual int FooId { get; set; }
   public virtual string FooName { get; set; }
}

Where there is a MyModel table that has Id, Name, and FooId as a foreign key into the Foo table. The Foo tables contains Id and FooName.

This problem is very similar to another post here: Nhibernate: join tables and get single column from other table but I am trying to figure out how to do it with FluentNHibernate.

I can make the Id, Name, and FooId very easily..but mapping FooName I am having trouble with. This is my class map:

public class MyModelClassMap : ClassMap<MyModel>
{
   public MyModelClassMap()
   {
      this.Id(a => a.Id).Column("AccountId").GeneratedBy.Identity();
      this.Map(a => a.Name);
      this.Map(a => a.FooId);

      // my attempt to map FooName but it doesn't work
      this.Join("Foo", join => join.KeyColumn("FooId").Map(a => a.FooName));
   }
}

with that mapping I get this error:

The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'join' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'joined-subclass, loader, sql-insert, sql-update, sql-delete, filter, resultset, query, sql-query' in namespace 'urn:nhibernate-mapping-2.2'.

any ideas?

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

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

发布评论

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

评论(1

原野 2024-09-10 17:17:48

我认为你在这里误解了一些东西。

您需要创建两个类

public class MyModel
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }

    public virtual Foo Foo { get; set; }
}

public class Foo
{
    public virtual int Id { get; set; }

    public virtual string FooName { get; set; }
}

以及它们的映射类,而不是在同一个类中包含 Id、Name、FooId 和 FooName:

public class MyModelMapping : ClassMap<MyModel>
{
    public MyModelMapping()
    {
        this.Id(x => x.Id);
        this.Map(x => x.Name);
        this.References(x => x.Foo);
    }
}

public class FooMapping : ClassMap<Foo>
{
    public FooMapping()
    {
        this.Id(x => x.Id);
        this.Map(x => x.FooName);
    }
}

这应该会有所帮助。

但请记住约定其他配置:)

I think you misunderstood something here.

Instead of having Id, Name, FooId and FooName in same class you need to create two classes

public class MyModel
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }

    public virtual Foo Foo { get; set; }
}

public class Foo
{
    public virtual int Id { get; set; }

    public virtual string FooName { get; set; }
}

And your mapping classes for these:

public class MyModelMapping : ClassMap<MyModel>
{
    public MyModelMapping()
    {
        this.Id(x => x.Id);
        this.Map(x => x.Name);
        this.References(x => x.Foo);
    }
}

public class FooMapping : ClassMap<Foo>
{
    public FooMapping()
    {
        this.Id(x => x.Id);
        this.Map(x => x.FooName);
    }
}

this should help.

But remember Convention other Configuration :)

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