3
votes

I'm struggling with the current scenario trying to create a mapping using a legacy database schema:

I have a pair of parent / child classes which are linked by a composite key, and I'm using FluentNHibernate to create the mappings and the association between them. Here are the classes:

/*Parent Class*/
public class Comprobante : Model<Comprobante>
{    
    public virtual IList<CuotaComprobante> Cuotas { get; protected set; }

    public virtual string NumeroComprobante { get; protected set; }
    public virtual string Tipo { get; protected set; }

    public Comprobante(){ }
}

/*Child Class*/
public class CuotaComprobante : Model<CuotaComprobante>
{
    public virtual Comprobante Comprobante { get; protected set; }

    public virtual string Estado { get; protected set; }
    public virtual DateTime FechaVencimiento { get; protected set; }

    /*!!!Want to get rid of this two properties in the child class*/                     
    public virtual string NumeroComprobante { get; protected set; }
    public virtual string Tipo { get; protected set; }


    public CuotaComprobante(){ }
}  

And here is the database schema:

Comprobante Table (gva12)
 - ID_GVA12
 - N_COMP (NumeroComprobante property in Comprobante class)
 - T_COMP (Tipo property in Comprobante class)

CuotaComprobante Table (gva46)
 - ID_GVA46
 - N_COMP
 - T_COMP

So, as you can see, N_COMP and T_COMP fields are part of the composite key (both ID_GVA12 and ID_GVA46 are not used for relationships in the database). I already tried using a HasMany relationship (shown in the code below), but it only performs the join using the T_COMP field, which gives me more results than expected. As you may have noticed, I just created the properties "NumeroComprobante" and "Tipo" in the child class for mapping purposes, but I really would like to get rid of them since it's information I already have in the parent class, or replace them with a reference to the Comprobante class.

HasMany(x => x.Cuotas)
            .KeyColumn("N_COMP").PropertyRef("NumeroComprobante")
            .KeyColumn("T_COMP").PropertyRef("Tipo")
            .Inverse(); 

So my question is: is there any way to acomplish that? I'm really out of ideas since I'm kinda newbie with mappings and googling around hasn't really provided any help.

Thanks in advance!

Mauricio

1
Please, provide full mappings for Comprobante and CuotaComprobante classes. - SHSE

1 Answers

3
votes

try

Component(x => x.ComponentContainingNumeroAndTipo, c =>
{
    c.Map(x => x.NumeroComprobante, "N_COMP");
    c.Map(x => x.Tipo, "T_COMP");
});

HasMany(x => x.Cuotas)
        .KeyColumns.Add("N_COMP", "T_COMP")
        .PropertyRef("ComponentContainingNumeroAndTipo")
        .Inverse();