1
I saw that an ASP.NET MVC project with C# has 2 magic tools that are Entityframework and Scaffolding. With them it is possible in a few minutes to have all the features of registration with in the database. Just bring the templates with Entity and then create a controller with scaffolding. Right? Good this is great for direct and simple registrations. But what if I want to insert in two or more related tables simultaneously in my bank? Example: Is it possible, with the same ease I create the INSERT/UPDATE, etc for these 3 tables in a single form? A single page where I fill all the fields click 1 button and tharam! I do the Insert in the 3 tables. Something similar to:
SELECT dbo.Tab_Aluno.*, dbo.Tab_Pessoa_Fisica.*, dbo.Tab_Contato.*
FROM dbo.Tab_Pessoa_Fisica
INNER JOIN dbo.Tab_Contato ON dbo.Tab_Pessoa_Fisica.Id_Contato = dbo.Tab_Contato.Id
INNER JOIN dbo.Tab_Aluno ON dbo.Tab_Pessoa_Fisica.Id = dbo.Tab_Aluno.Id_Pessoa_Fisica
The model would be:
public class CadastroAluno
{
public int CPF { get; set; }
public string Nome { get; set; }
public string Sexo { get; set; }
public DateTime DataDeNascimento { get; set; }
public string ContatoPrincipal { get; set; }
public string TelefonePrincipal { get; set; }
public string TelefoneSecundario { get; set; }
public string Email { get; set; }
public int Matricula { get; set; }
public byte Foto { get; set; }
public DateTime DataMatricula { get; set; }
public string Status { get; set; }
}
It has yes, basically you save the first object recovers in the instance of the Entity framework the id of the saved object to grab your id and use to insert in the next Insert and so on.
– Marco Souza
Thank you @Marconciliosouza. If I understand correctly you suggest that I create a model with all the fields I need. And when the user clicks the save button I treat each Sert separately. It’s not what I had in mind... but it can solve. But I didn’t quite understand this from "in the instance of the Entity framework itself the id of the saved object catch its id". Can you give me an example or a link with some article or tutorial? presiso of some reference of this part.
– AlamBique
have a good example here https://stackoverflow.com/a/42349763/2740371
– Marco Souza
if you need an example post your model.
– Marco Souza
@Marconciliosouza Please... would help me a lot. My template is exactly these three tables of the post. All fields except Id’s in Personal & Contact. There is an address table but you don’t need it... (I added the template in the post).
– AlamBique
This is more for a cabinet than for a model.
– Marco Souza
Kkkkkk! Sorry for the simplicity, besides being just an example, I’m beginner in MVC. I’m trying to develop my first application yet... take it easy.
– AlamBique
Okay, have you imported your database to your application using EF? it generates the template for you.
– Marco Souza