Normalization of tables and relationships

Asked

Viewed 717 times

4

I’m starting a project from scratch and in the middle of bank modeling, I had a problem. While building the models, I couldn’t do it the right way and I ended up getting confused.

I have student information, records and logins. Where I have several occurrences for a student and several occurrences for a login, to track which login is generating the occurrence.

But here is my problem: in my table of students I have various information such as student name, father name, mother name, parent name, contact phone, main phone, contact phone and alternative phone.

I thought about putting the parents' names on a different table, as well as the phones, to leave the tables with their respective objections, subjects. That is, organize the affairs. But in doing so, I made several mistakes in doing the scaffolding, because at first it only appeared as dropdown a phone and only the father’s name. Then missing data.

How would I do it? Would I leave all the information on one table? That is, would you delete these phone tables and parents, and leave this information in a single table, that of students? Or not, separating is right, creating these two tables? And how would I?

The models:

Pupil

 //Aqui começa a declaração normal dos atributos
    public long Id { get; set; }

    public string TipoEnsino { get; set; }

    public string Nome { get; set; }

    public string Endereco { get; set; }

    public DateTime DataNascimento { get; set; }

    public DateTime AnoLetivo { get; set; }

    public int Ano { get; set; }

    public string Turma { get; set; }

    public int NumeroChamada { get; set; }

    public string Foto { get; set; }

    public string Observacoes { get; set; }
    //Aqui termina 

    //Aqui começa os relacionamentos

        //Ocorrencias
        public ICollection<Ocorrencia> Ocorrencias { get; set; }

        //Pais
        public long PaisId { get; set; }
        public Pais Pais { get; set; }

        //Telefone
        public long TelefoneId { get; set; }
        public Telefone Telefone { get; set; }
    //Aqui termina

Parents

 //Aqui começa a declaração normal dos atributos
    public long Id { get; set; }

    public string NomePai { get; set; }

    public string NomeMae { get; set; }

    public string NomeResponsavel { get; set; }
    //Aqui termina a declaração

    //Aqui começa os relacionamentos

        //Aluno
        public long AlunoId { get; set; }
        public Aluno Aluno { get; set; }
    //Aqui termina

Telephone

//Aqui começa a declaração normal de atributos
    public long Id { get; set; }

    public string Contato { get; set; }

    public string Alternativo { get; set; }

    public string Responsavel { get; set; }
    //Aqui termina a declaração normal

    //Aqui começa os relacionamentos

        //Aluno
        public long AlunoId { get; set; }
        public Aluno Aluno { get; set; }
    //Aqui termina
  • 2

    It would be interesting to insert the full error message in the question and if possible its model classes and views.

  • So, man, the thing is, I went through the code here and it got messy. But I had relationship mistakes, where Entity didn’t know where it started or ended and then I had Migrations errors, due to the changes I made. But what puzzles me is: How do I mount the student table ? Do you separate this information, like parents' names and phone numbers, into a different table, or do I leave it all on a single table, that is, student’s table ? Because the way I did, I didn’t work. I did the parent tables and the phones and connected 1-1 with the student table. And then it generated me a view with dropdowns. And that’s not what I wanted...

  • You can ask your Models the question, even if they are messy?

  • @Ciganomorrisonmendez change made ! Sorry for the delay. But these are the models that are giving me a headache.

2 answers

7


I don’t see the need to separate the information from Student Parents, unless you want to make a table of Guardians, whose cardinality for Student is 1 for N.

In any case, I’ll assume you just want to register father and mother, then the entity Aluno gets like this:

[DisplayColumn("Nome")]
public class Aluno 
{
    [Key]
    public long AlunoId { get; set; }

    public TipoEnsino TipoEnsino { get; set; } //TipoEnsino é um Enum

    [Required]
    public string Nome { get; set; }

    public string Endereco { get; set; }

    public DateTime DataNascimento { get; set; }

    public DateTime AnoLetivo { get; set; }

    public int Ano { get; set; }

    public string Turma { get; set; }

    public int NumeroChamada { get; set; }

    public string Foto { get; set; }

    public string NomePai { get; set; }

    public string NomeMae { get; set; }

    public string NomeResponsavel { get; set; }

    [NotMapped]
    public HttpPostedFileBase ArquivoFoto { get; set; }

    public string Observacoes { get; set; }

    //Ocorrencias
    public virtual ICollection<Ocorrencia> Ocorrencias { get; set; }

    //Telefone
    public virtual ICollection<Telefone> Telefones { get; set; }
}

Phone changed to cardinality N:

[DisplayColumn("Numero")]
public class Telefone 
{
    [Key]
    public long Id { get; set; }

    public TipoTelefone TipoTelefone { get; set; }

    public string Numero { get; set; }

    //Aluno
    public long AlunoId { get; set; }
    public virtual Aluno Aluno { get; set; }
}
  • So this way, I could register several phones right ? To clarify: this Tipotelefone would be an Enum too right ? And I wouldn’t have to create a Controller to do the telephone manipulations, right ? Or would have ?

  • Yes, you could register as many phones as you want. TipoTelefone is an Enum. In your case it can have 3 values: Contact, Alternative and Responsible. The manipulation of phones you can do on own Controller of Student. I can indicate my answers in which have this manipulation well explained.

  • It would be very good if you could indicate these answers. Because by Controller student would be much easier for me and save time ! :)

  • The summary is here: http://answall.com/questions/38188/pr%C3%A1ticas-recomendas-para-inser%C3%A7%C3%A3o-altera%C3%A7%C3%A3o-e-exclus%C3%A3o-com-entityframework/38192#38192

  • Gypsy, I saw the links and such. But I could not see right. I would have to, in addition to installing the package, move both the controller, both in the view ? Could I have put an example of code that way in my context ?

  • @Érikthiago Here: http://answall.com/questions/8746/mestre-detailse-em-mvc-com-razor/8749#8749

  • So if I do it the way it is in this answer, it will work right ?

  • @Érikthiago Vai. Just put Aluno in place of Cliente. The logic is the same.

  • Easy then. I’ll do it here, in case I make some kind of mistake I’ll come back !

  • Gypsy, in this section 'if (shop.Phones != null)' this one shop represents which attribute ? The client ?

  • @Érikthiago Isso.

  • Gypsy, I had problems here... The partial does not appear in any way... I went to inspect the page here and it does not appear nor the code related to partial..

  • @Eric I think it’s time for a new question, no?

  • Do you think it best ?

  • Absolutely. Look at the size of this comment list. We’re already on another subject that isn’t necessarily within the scope of the question. Better separate.

  • All right, I’ve already put the question link here !

  • Gypsy, link here: http://answall.com/questions/39055/partial-n%C3%A3o-%C3%A9-rendered

Show 12 more comments

5

In relation to this, as the Gypsy said, unless the students do not have the relationship with parents but with guardians. If it is only parents, we know that a person can only have 1 father and 1 mother (in theory), so he could leave the information of each one together with the information of the Student. About the phones, by the ratio being from 1 to n I usually see in numerous applications the use of an external table for both phone and e-mail

  • Our dear, very good your explanation too. In addition to the reply of the Gypsy, yours helped me a lot to better understand this part of normalization !

Browser other questions tagged

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