Entity mapping oneToMany with Fluent Nhibernate

Asked

Viewed 282 times

0

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

I have the 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 it this way:

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 Customer object with the filled Meter list. I just make a:

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

And the list of meters is empty, even though there are records in the bank. I would be grateful for any help/suggestion.

  • It would be important for you to post the mistake you’re making. Don’t know Fluent or Nhibernate, but posting the error, colleagues here will better understand your problem. Make an edit and post the error.

  • Hi, there is no error (Exception). The query runs normally but does not return what I want. It is bugged.. have tried several ways to map and none of them solved the problem...

1 answer

0

I managed to solve...stay there the solution in case someone goes through the same problem.

I wanted to make a relationship between tables without them 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)

Thus, 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 you all!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.