Bag nhibernate with key null

Asked

Viewed 81 times

1

Hello,

Does anyone know if it is possible to use nhibernate Bag with Key Null?

In a table some records have several related records, one for many, but not always, sometimes a region will not have any.

I mapped by a bag but from the Foreign key error why the field in the related table will sometimes be null.

Is there any way to bring a collection of record that I need.

Follow the class code and mapping:

Class possessing the parent records:

public class Fatura
{
    public virtual long Id { get; set; }
    public virtual DateTime DtEmissao { get; set; }
    public virtual decimal VlrTotal { get; set; }
    public virtual Convenio Convenio { get; set; }
    public virtual IList<Receber> Receber { get; set; }

    public Fatura()
    {
        Receber = new List<Receber>();
    }

}

Class that holds the children records:

public class Receber
{
    public virtual long Id { get; set; }
    public virtual Fatura Fatura { get; set; }
    public virtual DateTime Pagamento { get; set; }
    public virtual decimal Valor { get; set; }
    public virtual decimal Juros { get; set; }
    public virtual decimal Multa { get; set; }
    public virtual decimal OutrosAcrescimos { get; set; }
    public virtual decimal Desconto { get; set; }
}

Mapping of the parent class:

public class FaturaMapping : ClassMapping<Fatura>
{
    public FaturaMapping()
    {
        Table("FATURA");

        Id(x => x.Id, x =>
        {
            x.Column("ID_FATURA");
        });

        Property(x => x.DtEmissao, x =>
        {
            x.Column("DTEMISSAO");
        });

        Property(x => x.VlrTotal, x =>
        {
            x.Column("VLRTOTAL");
        });

        ManyToOne(x => x.Convenio, x =>
        {
            x.Column("FK_CONVENIO");
            x.NotNullable(true);
        });

        Bag(x => x.Receber, x =>
        {
            x.Key(y =>
            {
                y.Column("FK_FATURA");
                y.NotNullable(false);
            });
        }, x => x.OneToMany());

    }
}
  • Edmar, could you edit your question and put to us your mapping code?

  • Hello Felipe, edited. Thank you.

No answers

Browser other questions tagged

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