Unsaved relationships

1

I have this question where thanks to the help of colleagues I was able to solve my situation. Well, at least in parts ... After making the adjustments pointed out, the data is being saved. But relationships are not being saved during data entry.

To make it clear, I make the relationships between several tables, where the relationship is 1: 1 between each table ...

Come on:

  • I have Table2 that relates to CustomerClient. Table 2 which relates to Table 3. Table 2 which relates to Table 4 and Table 2 which relates to Table 5.

  • I have Table3 that relates to Table2.

  • I have Table4 that relates to Table2.

  • I have Table5 that relates to Table2.

Okay, understood how relationships work, I did what is described in the question. I did the ViewModel, all right. Beauty.

The registration of the data is quiet, but when it is to relate the tables, this relationship is not being saved in the bank. The only relationship that is saved in the database is between Table2 and CliClient.

To get clearer, I'll post the pictures I have here.

Table2

Table3

Table4

Table5

Icheckedeverythingandthisasitshouldbe,butrelationshipsarenotbeingsaved...Doesanyoneknowwhythisishappening?

EDIT

MyViewModelcontroller:

//GET:Anaminese/CreatepublicActionResultCreate(){returnView(newAnamineseViewModel{CliCliente=newCliCliente(),Tabela2=newTabela2(),Tabela3=newTabela3(),Tabela4=newTabela4(),Tabela5=newTabela5(),});}//POST:Anaminese/Create[HttpPost]publicActionResultCreate(AnamineseViewModelanaminese){//TODO:Addinsertlogichereif(ModelState.IsValid){Tabela2tabela2=anaminese.Tabela2;Tabela3tabela3=anaminese.Tabela3;Tabela4tabela4=anaminese.Tabela4;Tabela5tabela5=anaminese.Tabela5;Tabela3.Tabela2=tabela2;Tabela4.Tabela2=tablea2;Tabela5.Tabela2=tabela2;db.CliCliente.Add(anaminese.CliCliente);db.Tabela2.Add(anaminese.Tabela2);db.Tabela3.Add(tabela3);db.Tabela4.Add(tabela4);db.Tabela5.Add(tabela5);db.SaveChanges();returnRedirectToAction("Index", "UsuUsuario");
        }

        return View(anaminese);
    }

My DbContext

  public DbSet<UsuUsuario> UsuUsuario { get; set; }

    //Criando no banco a tabela de perfil com seus campos
    public DbSet<PerPerfil> PerPerfil { get; set; }

    //Criando no banco a tabela de perfil com seus campos
    public DbSet<CliCliente> CliCliente { get; set; }

    public DbSet<Tabela2> Tabela2 { get; set; }

    public DbSet<Tabela3> Tabela3{ get; set; }

    public DbSet<Tabela4> Tabela4 { get; set; }

    public DbSet<Tabela5> Tabela5 { get; set; }

    //Atualizando o contexto sempre com o que há de mais novo
    //dessa forma o banco estara sempre atualizado
    public EntidadesContexto()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<EntidadesContexto, Configuration>());
    }
    
asked by anonymous 29.05.2016 / 05:55

2 answers

2

With the previous answers and in this question saying that tabela2 is related to tabela3 , tabela4 and tabela5 this code does not reflect the recording of the relationships actually. I believe it's a EntityFramework as% used, because of the command ORM that question .

The code would look like this at the time you enter:

[HttpPost]
public ActionResult Create(AnamineseViewModel anaminese)
{
    // TODO: Add insert logic here
    if (ModelState.IsValid)
    {
        CliCliente cliente = anaminese.CliCliente

        Tabela2 tabela2 = anaminese.Tabela2;

        cliente.Tabela2 = tabela2;  

        Tabela3 tabela3 = anaminese.Tabela3;
        Tabela4 tabela4 = anaminese.Tabela4;
        Tabela5 tabela5 = anaminese.Tabela4;

        tabela3.Tabela2 = tabela2;
        tabela4.Tabela2 = tabela2;
        tabela5.Tabela2 = tabela2;

        db.CliCliente.Add(cliCliente);      
        db.Tabela2.Add(tabela2);        
        db.Tabela3.Add(tabela3);        
        db.Tabela4.Add(tabela4);        
        db.Tabela5.Add(tabela5);        

        db.SaveChanges();

        return RedirectToAction("Index");
    }

    return View(anaminese);
}

If relationships are correct this will work.

    
29.05.2016 / 15:11
2

Logic will not work the way it is because you are not necessarily creating elements of Tabela2 , Tabela3 , Tabela4 and Tabela5 in every creation of CliCliente . But if they do not exist yet, this would already solve the problem:

db.CliCliente.Add(cliCliente);
db.SaveChanges();

This is because, starting with Entity Framework 6, the framework is smart enough to detect the dependencies between its entities. Therefore, you would not have to manually add all of them.

Still, if you want to do it this way, it's possible, but the result is a bit more complex and gives you a little more work.

First, add the Assembly System.Transactions to your project, if it is not already.

Next, you will have to use the transactional scope of the Entity Framework because we are going to do various database operations, and they must be executed atomically. If one fails, all must fail, in short.

[HttpPost]
public ActionResult Create(AnamineseViewModel anaminese)
{
    if (ModelState.IsValid)
    {
        using (var scope = new TransactionScope())
        {
            Tabela2 tabela2 = anaminese.Tabela2;
            db.Tabela2.Add(anaminese.Tabela2);
            db.SaveChanges();

            Tabela3 tabela3 = anaminese.Tabela3;
            Tabela3.Tabela2 = tabela2;
            db.Tabela3.Add(tabela3);
            db.SaveChanges();

            Tabela4 tabela4 = anaminese.Tabela4;
            Tabela4.Tabela2 = tabela2;
            db.Tabela4.Add(tabela4);
            db.SaveChanges();

            Tabela5 tabela5 = anaminese.Tabela5;
            Tabela5.Tabela2 = tabela2;
            db.Tabela3.Add(tabela5);
            db.SaveChanges();

            anaminese.CliCliente.Tabela2 = tabela2;
            anaminese.CliCliente.Tabela3 = tabela3;
            anaminese.CliCliente.Tabela4 = tabela4;
            anaminese.CliCliente.Tabela5 = tabela5;
            db.CliCliente.Add(anaminese.CliCliente);
            db.SaveChanges();

            scope.Complete();
        }

        return RedirectToAction("Index", "UsuUsuario");
    }

    return View(anaminese);
}
    
29.05.2016 / 20:58