Giter VIP home page Giter VIP logo

Comments (7)

leonardoporro avatar leonardoporro commented on June 4, 2024

Hi @andreamorello93,
At the first glance, it looks good, the only thing I've never tested is .UseModel()

Here, ensure that "entity" is set to the result of the mapping, as the input and mapped values are not the same entity (one was provided by you and the other one comes from the query or it's created by the mapper)

public async Task<TModel> Update(TModel entity) { //_context.Update(entity); entity = await _context.MapAsync<TModel>(entity); await _context.SaveChangesAsync(); return entity; }

I would need the model to give you more information.
Thanks for trying the library and for the feedback.

from detached-mapper.

andreamorello93 avatar andreamorello93 commented on June 4, 2024

Thank you @leonardoporro,

UseModel(); it's called Compiled Models, a new EFCore feature from 6+ that speeds up dbcontext on first query for huge dbcontexts.

Anyway, i've tried without it and the situation doesn't change.

it seems that everything is respected as you mentioned.

I've found that the entity where mapper throws an error, its a VIEW that has one of the sub entity related. quite strange.

Error:
"An item with the same key has already been added. Key: DAL.Models.RIS_MNT_AUTOMEZZI",

this is the entity i'm trying to update:

[Index("SEDILG_Codice_Fiscale", Name = "IX_SEDILG", IsUnique = true)]
public partial class SEDILG
{
    [Key]
    public int SEDILG_Id { get; set; }

    [Required]
    [StringLength(25)]
    [Unicode(false)]
    public string SEDILG_Codice_Fiscale { get; set; }

    public bool SEDILG_Codice_Fiscale_Errato { get; set; }

    [Required]
    [StringLength(25)]
    [Unicode(false)]
    public string SEDILG_Partita_Iva { get; set; }

    public bool SEDILG_Partita_Iva_Errata { get; set; }

    [Required]
    [StringLength(100)]
    [Unicode(false)]
    public string SEDILG_Ragione_Sociale { get; set; }

    public int SEDILG_IdIndirizzo { get; set; }

    public int? SEDILG_IdRecapito { get; set; }

    [Required]
    [Unicode(false)]
    public string SEDILG_Note_Memo { get; set; }

    [Required]
    [StringLength(35)]
    [Unicode(false)]
    public string SEDILG_CodAltreProc { get; set; }

    [Required]
    [StringLength(16)]
    [Unicode(false)]
    public string SEDILG_CASap { get; set; }

    [Required]
    [StringLength(50)]
    [Unicode(false)]
    public string SEDILG_Insert_Login { get; set; }

    [Precision(0)]
    public DateTime SEDILG_Insert_Data { get; set; }

    [Required]
    [StringLength(50)]
    [Unicode(false)]
    public string SEDILG_Update_Login { get; set; }

    [Precision(0)]
    public DateTime SEDILG_Update_Data { get; set; }

    [Required]
    public byte[] SEDILG_Versione { get; set; }

    [Required]
    [StringLength(50)]
    [Unicode(false)]
    public string SEDILG_CodImport { get; set; }

    [Required]
    [StringLength(50)]
    [Unicode(false)]
    public string SEDILG_CognomeRL { get; set; }

    [Required]
    [StringLength(50)]
    [Unicode(false)]
    public string SEDILG_NomeRL { get; set; }

    [Required]
    [StringLength(25)]
    [Unicode(false)]
    public string SEDILG_Codice_FiscaleRL { get; set; }

    public bool SEDILG_Codice_FiscaleRL_Errato { get; set; }

    public byte SEDILG_TipologiaDocumentoRL { get; set; }

    [Required]
    [StringLength(100)]
    [Unicode(false)]
    public string SEDILG_NumeroDocumentoRL { get; set; }

    public bool SEDILG_FlagWhitelist { get; set; }

    public bool SEDILG_FlagOffuscato { get; set; }

    [InverseProperty("AGENTI_IdSedeNavigation")]
    public virtual ICollection<AGENTI> AGENTI { get; set; } = new List<AGENTI>();

    [InverseProperty("ANAGRAFICA_IdSedeNavigation")]
    public virtual ICollection<ANAGRAFICA> ANAGRAFICA { get; set; } = new List<ANAGRAFICA>();

    [InverseProperty("COMANAGRAFICOKEYVALUE_IdSedeNavigation")]
    public virtual ICollection<COMANAGRAFICOKEYVALUE> COMANAGRAFICOKEYVALUE { get; set; } = new List<COMANAGRAFICOKEYVALUE>();

    [InverseProperty("COMNORMECERTIFICAZIONI_IdSedeNavigation")]
    public virtual ICollection<COMNORMECERTIFICAZIONI> COMNORMECERTIFICAZIONI { get; set; } = new List<COMNORMECERTIFICAZIONI>();

    [InverseProperty("INDIRIZZIFD_IdSedeNavigation")]
    public virtual ICollection<INDIRIZZIFD> INDIRIZZIFD { get; set; } = new List<INDIRIZZIFD>();

    [InverseProperty("INTERMEDIARI_IdSedeNavigation")]
    public virtual ICollection<INTERMEDIARI> INTERMEDIARI { get; set; } = new List<INTERMEDIARI>();

    [InverseProperty("PERSONALE_IdSedeTrasportatoreNavigation")]
    public virtual ICollection<PERSONALE> PERSONALE { get; set; } = new List<PERSONALE>();

    [InverseProperty("PRODUTTORI_IdSedeNavigation")]
    public virtual ICollection<PRODUTTORI> PRODUTTORI { get; set; } = new List<PRODUTTORI>();

    [InverseProperty("RUOLIAZ_IdSedeNavigation")]
    public virtual ICollection<RUOLIAZ> RUOLIAZ { get; set; } = new List<RUOLIAZ>();

    [ForeignKey("SEDILG_IdIndirizzo")]
    [InverseProperty("SEDILG")]
    public virtual INDIRIZZI SEDILG_IdIndirizzoNavigation { get; set; }

    [ForeignKey("SEDILG_IdRecapito")]
    [InverseProperty("SEDILG")]
    public virtual RECAPITI SEDILG_IdRecapitoNavigation { get; set; }

    [InverseProperty("SMALTITORI_IdSedeNavigation")]
    public virtual ICollection<SMALTITORI> SMALTITORI { get; set; } = new List<SMALTITORI>();

    [InverseProperty("SOGGETTI_IdSedeNavigation")]
    public virtual ICollection<SOGGETTI> SOGGETTI { get; set; } = new List<SOGGETTI>();

    [InverseProperty("TIAAUTOSMALTIMENTI_IDSedeSmaltNavigation")]
    public virtual ICollection<TIAAUTOSMALTIMENTI> TIAAUTOSMALTIMENTI { get; set; } = new List<TIAAUTOSMALTIMENTI>();

    [InverseProperty("TIALETTURETAGUTENZE_IdSedeNavigation")]
    public virtual ICollection<TIALETTURETAGUTENZE> TIALETTURETAGUTENZE { get; set; } = new List<TIALETTURETAGUTENZE>();

    [InverseProperty("TRASPORTATORI_IdSedeNavigation")]
    
public virtual ICollection<TRASPORTATORI> TRASPORTATORI { get; set; } = new List<TRASPORTATORI>();
}

this is the entity from DTO:

{
      agenti_ID: 9
      agenti_IdSede: 17747
      agenti_IdSedeNavigation: {
        sedilg_Id: 17747
        sedilg_Codice_Fiscale: "GGG"
        sedilg_Codice_FiscaleRL: "GGG"
        sedilg_Codice_Fiscale_Errato: false
        sedilg_Partita_Iva: "GGG"
        sedilg_Partita_Iva_Errata: false
        sedilg_Ragione_Sociale: "GGG"
        sedilg_NomeRL: "Nome"
        sedilg_CognomeRL: "Cognome"
        sedilg_Codice_FiscaleRL_Errato: false
        sedilg_TipologiaDocumentoRL: 1
        sedilg_NumeroDocumentoRL: "1"
        sedilg_FlagWhitelist: true
        sedilg_FlagOffuscato: false
        sedilg_IdIndirizzo: 286220
        sedilg_IdIndirizzoNavigation: {
          indirizzi_Id: 286220
          indirizzi_Indirizzo: "Via verdi"
          indirizzi_Civico: "2BIS"
          indirizzi_Cap: "04100"
          indirizzi_Localita: "Località"
          indirizzi_Stato: "IT"
          indirizzi_Riferimento: "Riferimento"
          indirizzi_CodImport: "Codice import"
          indirizzi_Subcivico: "Sub Civico"
          indirizzi_Interno: ""
          indirizzi_Scala: ""
          indirizzi_Piano: ""
          indirizzi_Colore: ""
          indirizzi_Insert_Login: "Insert Login"
          indirizzi_Insert_Data: "2023-05-31T22:00:00.000+02:00"
          indirizzi_Update_Login: "Update Login"
          indirizzi_Update_Data: "2023-05-31T22:00:00.000+02:00"
          indirizzi_Versione: [0, 0, 0, 0, 13, 64, 68, 100]
        }
        sedilg_Note_Memo: "Note memo"
        sedilg_CodAltreProc: "AAA"
        sedilg_CASap: "AAA"
        sedilg_CodImport: "AAA"
        sedilg_Insert_Login: "Insert Login"
        sedilg_Insert_Data: "2023-05-31T22:00:00.000+02:00"
        sedilg_Update_Login: "Update Login"
        sedilg_Update_Data: "2023-05-31T22:00:00.000+02:00"
        sedilg_Versione: [0, 0, 0, 0, 13, 64, 68, 102]
      }
      agenti_CodAltreProc: "AAA43"
      agenti_Ragione_Sociale: "RAG SOC"
      agenti_IdIndirizzo: 286219
      agenti_IdIndirizzoNavigation: {
        indirizzi_Id: 286219
        indirizzi_Indirizzo: "Via verdi"
        indirizzi_Civico: "2BIS"
        indirizzi_Cap: "04100"
        indirizzi_Localita: "Località"
        indirizzi_Stato: "IT"
        indirizzi_Riferimento: "Riferimento"
        indirizzi_CodImport: "Codice import"
        indirizzi_Subcivico: "Sub Civico"
        indirizzi_Interno: ""
        indirizzi_Scala: ""
        indirizzi_Piano: ""
        indirizzi_Colore: ""
        indirizzi_Insert_Login: "Insert Login"
        indirizzi_Insert_Data: "2023-05-31T22:00:00.000+02:00"
        indirizzi_Update_Login: "Update Login"
        indirizzi_Update_Data: "2023-05-31T22:00:00.000+02:00"
        indirizzi_Versione: [0, 0, 0, 0, 13, 64, 68, 99]
      }
      agenti_IdRecapito: 75553
      agenti_IdRecapitoNavigation: {
        recapiti_Id: 75553
        recapiti_Telefono: "357845"
        recapiti_Telefono1: "357845"
        recapiti_Cellulare: "357845"
        recapiti_Fax: "357845"
        recapiti_EMail: "Email"
        recapiti_PEC: "PEC"
        recapiti_Web: "Web"
        recapiti_CodImport: "Codice"
        recapiti_Insert_Login: "Insert Login"
        recapiti_Insert_Data: "2023-05-31T22:00:00.000+02:00"
        recapiti_Update_Login: "Update Login"
        recapiti_Update_Data: "2023-05-31T22:00:00.000+02:00"
        recapiti_Versione: [0, 0, 0, 0, 13, 64, 68, 101]
      }
      agenti_Zona: null
      agenti_Provv1: 0
      agenti_Provv2: 1.5
      agenti_Incasso: true
      agenti_Disattivo: false
      agenti_Data_Disattivazione: "2023-05-31T00:00:00.000+02:00"
      agenti_Note_Disattivazione: "Note disattivazione"
      agenti_Note_Memo: "Note disattivazione"
      agenti_Login_Disattivazione: "Login disattivazione"
      agenti_Insert_Login: "Insert Login"
      agenti_Insert_Data: "2023-05-31T22:00:00.000+02:00"
      agenti_Update_Login: "Update Login"
      agenti_Update_Data: "2023-05-31T22:00:00.000+02:00"
      agenti_Codice_Old: "HH765"
      agenti_Versione: [0, 0, 0, 0, 13, 64, 68, 103]
    }

And this is the view entity, that is mentioned in the error:

[Keyless]
public partial class RIS_MNT_AUTOMEZZI
{
    [...A LOT O FIELDS...]

    [Required]
    [StringLength(100)]
    [Unicode(false)]
    public string SEDILG_RAGIONE_SOCIALE { get; set; }

    public int SEDILG_ID { get; set; }

    [Required]
    [StringLength(60)]
    [Unicode(false)]
    public string ManutDescrizione { get; set; }
}

As you can see the view has a relation with entity SEDILG. But it doesn't appear anywhere in update object.

from detached-mapper.

andreamorello93 avatar andreamorello93 commented on June 4, 2024

UPDATE:

I've tried a smaller update object:

 entity: {
      agenti_ID: 9
      agenti_IdSede: 17747      
      agenti_CodAltreProc: "AAA43"
      agenti_Ragione_Sociale: "RAG SOC"
      agenti_IdIndirizzo: 286219      
      agenti_IdRecapito: 75553      
      agenti_Zona: null
      agenti_Provv1: 0
      agenti_Provv2: 1.5
      agenti_Incasso: true
      agenti_Disattivo: false
      agenti_Data_Disattivazione: "2023-05-31T00:00:00.000+02:00"
      agenti_Note_Disattivazione: "Note disattivazione"
      agenti_Note_Memo: "Note disattivazione"
      agenti_Login_Disattivazione: "Login disattivazione"
      agenti_Insert_Login: "Insert Login"
      agenti_Insert_Data: "2023-05-31T22:00:00.000+02:00"
      agenti_Update_Login: "Update Login"
      agenti_Update_Data: "2023-05-31T22:00:00.000+02:00"
      agenti_Codice_Old: "HH765"
      agenti_Versione: [0, 0, 0, 0, 13, 64, 68, 103]
    }

But still get the same error

from detached-mapper.

andreamorello93 avatar andreamorello93 commented on June 4, 2024

@leonardoporro any suggestion? are [Aggregation] and [Composition] mandatory with autogenerated DAL?

from detached-mapper.

leonardoporro avatar leonardoporro commented on June 4, 2024

Sorry for the long long long delay. I've been away due to personal problems.

That's a large entity indeed.

At a first glance, given the message error and the [Keyless] attribute, I guess that the mapper tries to create an entity where it should update it.

I'm pretty sure there is no code to support keyless entities.

This library is mostly based in the class model, ocassionally getting info from EF to see what property is a key. But it doesn't handle shadow properties or any other inner EF concept.

By this time, if you didn't give up, please send me a small console app or unit test with your DbContext and failing entities and I would love to debug it and propose a solution.

Thanks

from detached-mapper.

leonardoporro avatar leonardoporro commented on June 4, 2024

There is an add-on to handle HotChocolate Optional<> type.
So it maps the value only if it exists in JSON.
Cool for PATCH operations.

https://www.nuget.org/packages/Detached.Mappers.HotChocolate

https://github.com/leonardoporro/Detached-Mapper/blob/main/test/Detached.Mappers.HotChocolate.Tests/OptionalSourceTests.cs

from detached-mapper.

andreamorello93 avatar andreamorello93 commented on June 4, 2024

Thank you @leonardoporro , i've solved creating recursively the IQueryable based on input entity, fetching it, than copying properties from input entity to db entity

from detached-mapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.