Hello,
Does anyone know if it's possible to use nHibernate's Bag with Key Null?
In a table some records have several related records, one for many, but not always, sometimes a regret will not have any.
I made the mapping by a bag but the foreign key error because the field in the related table will sometimes be null.
Is there any way to bring a registry collection this way that I need.
Here is the code for classes and mappings:
Class that has 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 has the child 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; }
}
Parent class mapping:
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());
}
}