Unsaved relationships

Asked

Viewed 85 times

1

Have that question where thanks to the help of colleagues I managed to resolve my situation. Well, at least in parts... After making the adjustments pointed out, the data is being saved. But the relationships are not being saved while registering the data.

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

Come on:

  • I have Table 2 which relates to Clicliente. Table 2 which relates to Table 3. Table 2 relating to Table 4 and Table 2 relating to Table 5.

  • I have Table 3 which relates to Table 2.

  • I have Table 4 which relates to Table 2.

  • I have Table 5 which relates to Table 2.

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 database. The only relationship that is saved in the bank is between Table2 and Clicliente.

To be clear, I’m going to post here the images I have.

Table2

inserir a descrição da imagem aqui

Table3

inserir a descrição da imagem aqui

Table4

inserir a descrição da imagem aqui

Table5

inserir a descrição da imagem aqui

I have checked everything and this as it should be, but relationships are not being saved... Does anyone know why this is happening?

EDIT

My Viewmodel controller:

 // GET: Anaminese/Create
    public ActionResult Create()
    {
        return View(new AnamineseViewModel
        {
            CliCliente = new CliCliente(),
            Tabela2 = new Tabela2(),
            Tabela3 = new Tabela3(),
            Tabela4 = new Tabela4(),
            Tabela5 = new Tabela5(),
        });
    }

    // POST: Anaminese/Create
    [HttpPost]
    public ActionResult Create(AnamineseViewModel anaminese)
    {
        // TODO: Add insert logic here
        if (ModelState.IsValid)
        {
            Tabela2 tabela2 = anaminese.Tabela2;
            Tabela3 tabela3 = anaminese.Tabela3;
            Tabela4 tabela4 = anaminese.Tabela4;
            Tabela5 tabela5 = 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();
            return RedirectToAction("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>());
    }
  • The relationship is all 1 to 1?

  • This @João. They are all one to on account of the need of the same project...

  • Perfect, it was my question

2 answers

2


With the previous answers and this question saying that the tabela2 relates to the tabela3, tabela4 and tabela5 this code does not reflect the recording of relationships really. I believe to be a EntityFramework the ORM used because of the command SaveChanges of that question.

The code would be 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 the relationships are correct this will work.

  • Man, it didn’t work... =(

  • What are/are the errors ? Put all the classes in this project in your well-organized question

  • Shows no error... Saves everything right, but does not save the Ids, like what happens in the images. = (. Which classes you want?

  • Well, I need to see the codes of this class + the class that configures or Dbcontext, if you can and want to paste organized all classes!

  • You speak my viewmodel controller and Dbcontext?

  • yes all tables etc etc

  • My models are great, better go to chat no?

Show 3 more comments

2

Logic won’t work the way it is because you’re not necessarily creating elements of Tabela2, Tabela3, Tabela4 and Tabela5 throughout the creation of CliCliente. But if they don’t exist yet, this would already solve the problem:

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

This is because, from Entity Framework 6, the framework is smart enough to detect dependencies between its entities. Therefore, you wouldn’t need to add all of them manually.

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

First, add Assembly to your project System.Transactions, if you’re not already.

Then, you will have to use the transactional scope of the Entity Framework, as we will do several database operations, and they need to be executed in an atomic way. If one of them 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);
}

Browser other questions tagged

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