ASP.NET MVC Entity - Scaffolding for more than one table simultaneously

1

I've seen 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 registration features with in the database. Just bring the models with Entity and then create a controller with the scaffolding. Right? Well this is great for direct and simple entries. But what if I want to insert into 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 on 1 button and tharam! I insert it into 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 template 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; }
    }
    
asked by anonymous 01.08.2017 / 19:50

2 answers

2
Assuming that in your model, in class PessoaFisica has a Contato property, you can add everything at once, so Entity will take care of the transaction for you, in case of error in one of inserts it will not save anything, it's safer

public class Contato
{
    public int Id { get; set; }
    public string ContatoPricipal { get; set; }
    public string Email { get; set; }
    public string Telefone_Principal { get; set; }
    public string Telefone_Secundario { get; set; }
}

public class PessoaFisica
{
    public int Id { get; set; }
    public string Cpf { get; set; }
    public DateTime Dt_Nascimento { get; set; }

    public int ContatoId { get; set; }
    public Contato Contato { get; set; }
}

In this step, when creating the object of PessoaFisica is already added Contato , then call SaveChanges() , both objects will be added and already related

using (var ctx = new SeuContexto())
{
    var pessoaF = new PessoaFisica
    {
        Cpf = "848.588.866-85",
        Dt_Nascimento = DateTime.Now,
        Contato = new Contato
        {
            ContatoPricipal = "sei la",
            Email = "[email protected]",
            Telefone_Principal = "99999999",
            Telefone_Secundario = "8888888",

        }
    };
    ctx.PessoaFisica.Add(pessoaF);
    ctx.SaveChanges();
}
    
15.08.2018 / 14:15
1

You would save your data like this;

using (var ctx = new stackoverflowEntities())
{
    var contato = new Contato();
    contato.ContatoPricipal = "sei la";
    contato.Email = "[email protected]";
    contato.Telefone_Principal = "99999999";
    contato.Telefone_Secundario = "8888888";

    ctx.Contato.Add(contato);
    ctx.SaveChanges();

    var pessoaF = new PessoaFisica();
    pessoaF.cpf = "848.588.866-85";
    pessoaF.Dt_Nascimento = DateTime.Now;
    pessoaF.ContatoId = contato.ContatoId; // aqui ta a magica .... 

    ctx.PessoaFisica.Add(pessoaF);
    ctx.SaveChanges();

}

First you save the Contact and after doing the SaveChanges the context saves the id that was generated so you use for the tables that have dependency on it.

    
02.08.2017 / 17:54