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
Table3
Table4
Table5
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?
– user46523
This @João. They are all one to on account of the need of the same project...
– Érik Thiago
Perfect, it was my question
– user46523