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>());
}