1

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

advapi
  • 3,661
  • 4
  • 38
  • 73

1 Answers1

6

I want to say that your issues come from trying to work with linq2db in the same way as people usually do with EF, but linq2db have no such tight coupling with data model (no change tracking, no automatic load of references). It will be easier with linq2db if you will separate mappings for database objects from data model of your application.

Regarding your second question about InsertWithIdentity - this API doesn't support setting of identity property, so you need to do it yourself if you need your object updated with generated property. You can create feature request for such functionality.

Regarding setting both referenced object value and foreign key field value question. There is nothing in linq2db that tells you to set any of those - if you do it then it is something you need for your application. If you want to know how linq2db use those two properties then:

  • FK field property is a direct mapping to FK table column and value from this property used in queries, e.g. for insert/update operations. Also this property populated with value on select.

  • Association property have two roles. First is an easy way to define join operation in linq query. Second is to load value for this property on select using LoadWith method. linq2db don't use value of this property for anything else.