FluentNHibernate, getting 1 column from another table

We use FluentNHibernate, and we encountered a problem when our object model requires data from two tables:

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 in the Foo table. Foo tables contain Id and FooName.

This problem is very similar to another entry here: Nhibernate: join tables and get one column from another table , but I'm trying to figure out how to do this using FluentNHibernate.

I can do Id, Name and FooId very easily .. but displaying FooName I have problems. 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 this mapping, I get this error:

'class' 'urn: nhibernate-mapping-2.2' 'join' 'urn: nhibernate-mapping-2.2'. : "join-subclass, loader, sql-insert, sql-update, sql-delete, filter, resultset, query, sql-query" "urn: nhibernate-mapping-2.2".

?

+3
1

, - .

, Id, Name, FooId FooName ,

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; }
}

:

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);
    }
}

.

, :)

0

Source: https://habr.com/ru/post/1747578/


All Articles