I've moved an old portion of code from EF to linq2db and when I had to add a new item with an identity that was referenced by inner element I had to perform the following code
if (axAnag == null)
{
axAnag = new DataModels.AX();
var idAnag= context.InsertWithInt32Identity(axAnag);
axAnag.IdAnagrafica = idAnag;
}
and below in the code
fk.AxAnagraficaAssicurati = axAnag;
fk.StatusProtocollo = statusProtocollo;
fk.DataEsclusione = newAnagrafica.DataEsclusione ?? condizione.DataAnnullamento;
fk.DataInclusione = newAnagrafica.DataInclusione;
fk.DerogaEta = newAnagrafica.DerogaEta;
fk.ProgrCategoria = condizione.ProgessivoCategoria;
fk.IdCondizione = condizione.Id;
fk.IdCategoria = condizione.IdCategoria;
fk.DataOperazioneInclusione = DateTime.Now;
fk.HYPER = newAnagrafica.HyperService;
//GPA ticket 2017-0017962
fk.LimiteEtaSuperato = newAnagrafica.IsLimiteEtaSuperato;
//GPA cr24
fk.FlagConteggioRegolazionePremio = newAnagrafica.FlagConteggioRegolazionePremio;
fk.IdAnagrafica = axAnag.IdAnagrafica;
What it's not so clear to me is why I have to add the id and the whole referenced item?
fk.AxAnagraficaAssicurati = axAnag;
fk.IdAnagrafica = axAnag.IdAnagrafica;
In the scaffolded model I've
[Association(ThisKey = "IdAnagrafica", OtherKey = "IdAnagrafica", CanBeNull = true, Relationship = Relationship.ManyToOne, KeyName = "FK_BENEFIT_FK_ANAGRAFICA_ASS_POLIZZE_ANAGRAFICA", BackReferenceName = "BenefitFkAnagraficaAssPolizze")]
public AxAnagraficaAssicurati AxAnagraficaAssicurati { get; set; }
Wouldn't it be enough to put one? and also related to the insert of the axAnag
is there a way I can avoid to do
var idAnag= context.InsertWithInt32Identity(axAnag);
axAnag.IdAnagrafica = idAnag;
Thanks