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

Asked

Viewed 733 times

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: inserir a descrição da imagem aqui 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.

  • 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.

  • have a good example here https://stackoverflow.com/a/42349763/2740371

  • if you need an example post your model.

  • @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).

  • This is more for a cabinet than for a model.

  • 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.

  • Okay, have you imported your database to your application using EF? it generates the template for you.

Show 3 more comments

2 answers

2


Supposing in your model, in the class PessoaFisica own a property Contato, you can add everything at once, so the Entity will handle the transaction for you in case of an error in one of the inserts, won’t 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; }
}

At this stage, by creating the object of PessoaFisica is already added the Contato, there call the 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();
}

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 then after doing Savechanges the context stores the id that was generated then you use for the tables that have dependency on it.

  • Thank you very much my friend! You really helped a lot.

  • Need to use 2 ctx.SaveChanges(); ? Only use at the end of the line that already does both (Contact and Personal).

Browser other questions tagged

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