Replicate/Copy elements from multiple tables

Asked

Viewed 776 times

6

I have the following situation: The user has defined an establishment, where he has filled in data in two tables, and intends to replicate/copy all the same data, changing only the id of the establishment. I have the tables:

inserir a descrição da imagem aqui

The table Acidezimpurezahumidade_tbl or AIH as I have in the project, receives as a foreign key the Servicosid table.

In my function to replicate/copy the data, I receive an integer that contains the id of the establishment (idServicoIDSelected) that I will copy and a list with the id of the establishments (listEstab) to copy to:

[AcceptVerbs(HttpVerbs.Get)]
    public JsonResult CriarCopiarEstab(List<int> listaEstab, int idServicoIDSelected, string serie, int numDoc)
    {
        try
        {
            var dados = db.DadosComerciais.Where(d => d.Serie == serie && d.NumDoc == numDoc).FirstOrDefault();
            var servico = db.Servicos.Find(idServicoIDSelected);
            var aih = db.AIH.Where(a => a.ServicosID == servico.ServicosID).ToList();
            foreach (var item in listaEstab)
            {
                var newServico = new Servicos();
                newServico = servico;
                newServico.DadosComerciais = db.DadosComerciais.Find(serie, numDoc);

                db.Servicos.Add(newServico);
                db.SaveChanges();

                var newAih = new List<AIH>();
                foreach (var item2 in aih)
                {
                    var newElementoAih = new AIH();
                    newElementoAih.AcidezDesconto = item2.AcidezDesconto;
                    newElementoAih.AcidezMax = item2.AcidezMax;
                    newElementoAih.AcidezTaxaExtraEur = item2.AcidezTaxaExtraEur;
                    //newElementoAih = item2; //TAMBEM JÁ EXPERIMENTEI ASSIM
                    db.AIH.Add(newElementoAih);
                    db.SaveChanges();
                }
            }
            return Json(JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json(new { Result = "ERROR", Message = ex.Message }, JsonRequestBehavior.AllowGet);
        }
    }

Where I got the problem? I can’t replicate the data, get the servicosID corresponding to be inserted in the AIH table. If replicating only the Services table works well, now if I start copying the lists I always get the error:

Invalidoperationexception was Caught The changes to the database Were Committed successfully, but an error occurred while updating the Object context. The Objectcontext Might be in an inconsistent state. Inner Exception message: A referential Integrity Constraint Violation occurred: The Property values that define the referential constraints are not consistent between principal and dependent Objects in the Relationship.

  • On which line exactly the error occurs?

  • Just when I do saveChanges() table of services

  • @Cesarmiguel Have you found a solution to the problem? You can post an answer with him for the staff to vote (you can even accept it as correct.

1 answer

1

This definition:

var newServico = new Servicos();
newServico = servico;
newServico.DadosComerciais = db.DadosComerciais.Find(serie, numDoc);

You are wrong. You are defining two objects of the type Servicos with the same ServicoID. The Entity Framework understands that they are two different objects, but SQL Server understands that the objects are equal.

I don’t know what your database scheme looks like, but the code supposing the use of Identity in the column ServicoID is more or less like this:

var newServico = new Servicos();
newServico.NumDocumento = servico.NumDocumento;
newServico.Serie = servico.Serie;
newServico.DadosComerciais = db.DadosComerciais.Find(serie, numDoc);
// Preencha as demais properties manualmente. Não preencha ServicoID

db.Servicos.Add(newServico);
db.SaveChanges();
  • The strange thing is that when I only record the Services table it holds well. But now if you see that there is data in the other table, and go through the list to save it is that gives error. I’ve already checked the problem. I did the following: I have the services table data saved first from javascript, and then at the end of these data be saved, command (also from javascript) save the data list in the AIH table

Browser other questions tagged

You are not signed in. Login or sign up in order to post.