OneToMany entity mapping with Fluent NHibernate

0

Gentlemen, my problem is apparently simple, I must be doing or forgetting something and I just can not see the error. Can you help me?

I have class Cliente :

public class Cliente {

        public Cliente () { }
        public virtual int ClienteId { get; set; }  
        public IList<Medidor> ListaMedidores { get; set; }   
        public virtual string NumeroMedidor { get; set; }       
}

And the class Medidor

public class Medidor
{
        public Medidor() { }
        public virtual string NumeroMedidor { get; set; }
        public virtual string MarcaMedidor { get; set; }
        public virtual Cliente Cliente { get; set; }
}

I tried to map as follows:

public ClienteMap()
{
        Map(x => x.NumeroMedidor).Column("CORE_NUMERO_MEDIDOR");
        HasMany(x => x.ListaMedidores).KeyColumn("NUMERO_MEDIDOR").Inverse().Cascade.All();
}


public MedidorMap()
{
        Table("medidor");
        LazyLoad();

        Id(x => x.NumeroMedidor).Column("NUMERO_MEDIDOR");
        Map(x => x.TipoMedidor).Column("TIPO_MEDIDOR");
        References(x => x.Cliente).Column("CORE_NUMERO_MEDIDOR");
}

My goal is to bring the Client object with the completed Meter list. I simply do one:

Session.Query<Cliente>().Fetch(x => x.ListaMedidores).ToList();

And the meter list comes empty even though you have records in the bank. I'll be grateful for any kind of help / suggestion.

    
asked by anonymous 08.07.2015 / 14:05

1 answer

0

I was able to solve it ... that is the solution if someone has the same problem.

I wanted to make a relationship between tables without being indexed by their PKs and FKs.

Follows:

public class Cliente
{    
    public Cliente () { }
    public virtual int ClienteId { get; set; }  
    public IList<Medidor> ListaMedidores { get; set; }   
    public virtual string NumeroMedidor { get; set; }       
}
public class Medidor
{
    public Medidor() { }
    public virtual string NumeroMedidor { get; set; }
    public virtual string MarcaMedidor { get; set; }
}

public class ClienteMap : ClassMap<Cliente>
{
    public ClienteMap()
    {
        Map(x => x.NumeroMedidor).Column("NUMERO_MEDIDOR");    
        HasMany(x => x.ListaMedidores)
            .KeyColumns.Add("NUMERO_MEDIDOR")
            .Table("MEDID")
            .PropertyRef("CoreNumeroCliente")
            .Cascade.All();
    }
}

public class MedidorMap : ClassMap<Medidor>
{
    public MedidorMap()
    {
        LazyLoad();

        Id(x => x.NumeroMedidor).Column("NUMERO_MEDIDOR");
        Map(x => x.MarcaMedidor).Column("MARCA_MEDIDOR");
        [...] //outras propriedades
    }
}

Session.Query<CorteReligacao>()
                .Fetch(x => x.ListaMedid)

This way, I have the same expected result as expected in the bank:

NUMERO_MEDIDOR MARCA_MEDIDOR
3569371        general_motors
3569371        kia
3569371        FIAT

A special thanks to @Radim Köhler, who helped me a lot. Good luck to all!

    
09.07.2015 / 14:05