none
Convert base class to derived class using EntityFramework RRS feed

  • Question

  • I have a base class Contatto and a derived class Allievo designed in my EntityFramework model.
    In my MVC website the user can create a new Contatto or a new Allievo but, sometimes, a Contatto could become an Allievo.
    How can i convert my Contatto into Allievo and save it in my database?

    Thank you.

    • Moved by CoolDadTx Monday, February 27, 2017 3:04 PM EF related
    Monday, February 27, 2017 11:09 AM

All replies

  • Ok. Thanks.
    Monday, February 27, 2017 2:33 PM
  • I have a base class Contatto and a derived class Allievo designed in my EntityFramework model.
    In my MVC website the user can create a new Contatto or a new Allievo but, sometimes, a Contatto could become an Allievo.
    How can i convert my Contatto into Allievo and save it in my database?

    Thank you.

    Monday, February 27, 2017 2:34 PM
  • Hi M Paga,

    Based on your description, I'm sure what causes the issue, could you please provide a bit more information, such as related code and error message.

    In addition, about EF Inheritance, please refer to the following articles.

    https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph

    https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt

    https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 28, 2017 6:42 AM
    Moderator
  • I'm sorry but i can't post images because my account is still not verified.

    In Sql i have 2 tables: ContattoSet and ContattoSet_Allievo

    When i create a new Allievo, in sql i find 1 new record in ContattoSet and 1 new record with same Id in ContattoSet_Allievo.

    When i create a new Contatto, in sql i find 1 new record in ContattoSet.

    But how can i do if i would convert my existing Contatto into an Allievo? Is this the correct approach to manage a new Contact and, if needed, change it in a "Customer"?

    I've tried to manage the edit of Allievo in this way:

    // GET: Allievoes/Edit/5
            public ActionResult Edit(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Allievo allievo = new Allievo();
                var contatto = db.ContattoSet.Find(id);
                if (contatto is Allievo)
                {
                    allievo = (Allievo)contatto;
                }
                else
                {
                    allievo.Id = contatto.Id;
                    allievo.Nominativo = contatto.Nominativo;
                    allievo.Telefono = contatto.Telefono;
                    allievo.Email = contatto.Email;
                    allievo.NoteContatto = contatto.NoteContatto;
                    allievo.DataCreazione = contatto.DataCreazione;
                    allievo.DataModifica = contatto.DataModifica;
                    allievo.Scuola_codice = contatto.Scuola_codice;
                }
                if (allievo == null)
                {
                    return HttpNotFound();
                }
                return View(allievo);
            }

    I'm trying to map a new Allievo with existing Contatto fields and specially keeping the Id, but when i try to post the edit:

    // POST: Allievoes/Edit/5
            // Per proteggere da attacchi di overposting, abilitare le proprietà a cui eseguire il binding. 
            // Per ulteriori dettagli, vedere http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit([Bind(Include = "Id,Nominativo,Telefono,Email,DataCreazione,DataModifica,Scuola_codice,NumeroRegistro,DataIscrizione")] Allievo allievo)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(allievo).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index", new { Scuola_codice = allievo.Scuola_codice });
                }
                return View(allievo);
            }

    i receive the error

    Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.


    Thank you



    • Edited by M Paga Tuesday, February 28, 2017 9:25 AM
    Tuesday, February 28, 2017 9:24 AM
  • Hi M Paga,

    Please convert allievo object to contatto object

    if (ModelState.IsValid)
    {
       var contatto = new ContattoSet(){ id = allievo.id, ...};
       db.Entry(contatto).State = EntityState.Modified;
       db.SaveChanges();
       return RedirectToAction("Index", new { Scuola_codice = allievo.Scuola_codice });
    }
    

    For more information, please refer to:

    https://msdn.microsoft.com/en-us/library/jj592676(v=vs.113).aspx

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 1, 2017 7:56 AM
    Moderator
  • Thanks Cole, but this way I only changed the Contatto fields and the object does not become an Allievo.

    In my EF Allievo and Contatto have a link of type inherit

    ----------------------------------------------------------------------------------------------------------------------------

    I've choosen to use a "Type" field in a unique table.

    Thanks anyway

    • Edited by M Paga Wednesday, March 1, 2017 1:23 PM
    Wednesday, March 1, 2017 8:45 AM
  • Hi M Paga,

    Could you please share a complete code, such as DBContext, related Model Class, Which I could reproduce your issue on my side, I will try my best to find a solution to resolve it.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 2, 2017 8:22 AM
    Moderator