Modeling of Models 1 to n

Asked

Viewed 85 times

2

I’m modeling a system for academies. I created 2 models,

Modality

 public class Modalidade
    {
        [Key]
        public int ModalidadeId { get; set; }

        [MaxLength(200)]
        public string Nome { get; set; }

        [Newtonsoft.Json.JsonIgnoreAttribute]
        [ForeignKey("ModalidadeId")]
        public virtual Horario Horarios { get; set; }
}

Schedule

 public class Horario
    {
        [Key]
        public int HorarioId { get; set; }


        public int ModalidadeId { get; set; }


        public byte HoraInicial { get; set; }

        public byte HoraFinal { get; set; }


        [Newtonsoft.Json.JsonIgnoreAttribute]
        public virtual ICollection<Modalidade> Modalidade { get; set; }
    }

I summarized, but I think about the time put the days of the week (mon, ter, quar, etc.) and also the Professorid, since a single mode can have different teachers, ex: Second, mode = Karate, room=1 teacher=1, Fourth, Mode = Karate, room=2, teacher=2;

Summary: I can have several schedules for 1 single mode.

My question: When creating the modality first, it would give error in the database, because the table Horario needs the Modalidadeid, so first create the (strange) time I would not even know what the Id?

I tried to execute and of course it was a mistake:

    var mod = new Modalidade
    {
        Nome = @"TKD",
        DataCadastro = DateTime.Now,
        Excluido=false
    };
    var hor = new Horario
    {
        HoraFinal = 9,
        HoraInicial = 8,
        ModalidadeId = mod.ModalidadeId
    };
    db.Modalidades.Add(mod);
    db.Horarios.Add(hor);
    db.SaveChanges();

Cannot convert an object of type 'Arena.Models.Mode' in kind 'System.Collections.Generic.Icollection`1[Arena.Models.Modality]'.

  • I would make many for many. why? a modality for having several schedules and a schedule can be in several modality?

  • is well founded, but then I thought of putting the Professor linked to the Schedule. ?

  • You have to think about the design of the tables first and then reflect on the class model. It has to analyze teacher, modality and schedule, because teacher can not shock the schedules.

  • I need a help.. rs Let’s try in my case above, the error pq?

  • 2

    Good I will comment on this that I see the relationship is reversed, if it is 1 for many 1 mode has several times and a time belongs to a mode ( ai that is, It is also wrong should be many for many) but, in the current model is reversed.

  • 2

    I’m not sure I quite understand your question. But if you had a register for Classroom, Teacher, Modality and Schedule and, with this, create an associative table SalaProfessorModalidadeHorario and so you would have the distinct registrations and an associative table to do what you want. Not to mention it would be easy to remove the reports in that way.

  • 1

    Now, creating a Model only for Schedules can cause you trouble, because editing the time of one will edit all the others.

Show 2 more comments

2 answers

3


@dorathoto seeing his idea of having an entity Horario, with beginning and end I believe that its main problem is that the Modalidade who has a ICollection of schedules and not unlike how you mapped.

Following your idea, I mapped 4 entities Modalidade, Professor, Sala and Horario, the latter being an associative entity among the other 3.

Leaving in the following form my example

public class Modalidade
{
    [Key]
    public int ModalidadeId { get; set; }

    [MaxLength(200)]
    public string Nome { get; set; }

    // modalidade tem horários
    public ICollection<Horario> Horarios { get; set; }
}

public class Professor
{
    [Key]
    public int ProfessorId { get; set; }

    [MaxLength(200)]
    public string Nome { get; set; }

    // Professor tem horários
    public ICollection<Horario> Horarios { get; set; }
}

public class Sala
{
    public int SalaId { get; set; }

    public string Nome { get; set; }

    //Sala tem horários
    public ICollection<Horario> Horarios { get; set; }
}

public class Horario
{
    [Key]
    public int ModalidadeProfessorSalaHorarioId { get; set; }

    public DateTime DataHoraInicio { get; set; }

    public DateTime DataHoraFim { get; set; }

    public int ModalidadeId { get; set; }

    public int ProfessorId { get; set; }

    public int SalaId { get; set; }

    //Horário tem Modalidade
    public virtual Modalidade Modalidade { get; set; }

    //Horário tem professor
    public virtual Professor Professor { get; set; }

    //Horário tem Sala
    public virtual Sala Sala { get; set; }
}

Dessa forma que ficou o meu modelo ER

Summary: I can have several schedules for 1 single mode.

The way I mapped it would be contemplated. Of course some checks would be necessary to avoid the same room being occupied more than once in the same time interval. Same check would have to be done for the teacher.

My doubt: When creating the modality first, it would give error in the Bank of data, because the Time table needs the Modality, then create first the time (strange) I still wouldn’t know what the Id?

This doubts part of the problem of your mapping, to have a list of times in the mode and not the entity Horario have a list of modalities.

3

It would not be the case to have the modeling below?

Thinking OO, we have a teacher object, a schedule and a mode.

Where do they meet? In my opinion in Class.

Each class is unique in the week and it has 1 teacher, 1 time and 1 mode.

Ex:

Professor joao = new Professor {Nome = "João" };
Horario seg8as9 = new Horario {DiaDaSemana="Seg", HoraInicial=8, HoraFinal=9};
Modalidade tkd = new Modalidade {Nome="TKD"};

Aula aulaTKDSeg8as9ProfJoao = new Aula {Professor=joao, Horario=seg8as9, Modalidade=tkd};

db.Professor.Add(joao)
db.Horarios.Add(seg8as9);
db.Modalidades.Add(tkd);
db.Aulas.Add(aulaTKDSeg8as9ProfJoao);

db.SaveChanges();

I did the head code, I didn’t test it so it’s possible that adjustments are necessary, it was just to give an idea.

public class Professor {
    [Key]
    public int Id {get; set;}
    public string Nome {get; set;}
}

public class Horario {
    [Key]
    public int Id {get; set;}
    public string DiaDaSemana {get; set;}
    public byte HoraInicial {get; set;}
    public byte HoraFinal {get; set;}
}

public class Modalidade {
    [Key]
    public int Id {get; set;}
    public string Nome {get; set;}
}


public class Aula {
    [Key]
    public int Id {get; set;}

    public int ProfessorId {get; set;}

    public int HorarioId {get; set;}

    public int ModalidadeId {get; set;}

    [Newtonsoft.Json.JsonIgnoreAttribute]
    public virtual Professor Professor { get; set; }

    [Newtonsoft.Json.JsonIgnoreAttribute]
    public virtual Horario Horario { get; set; }

    [Newtonsoft.Json.JsonIgnoreAttribute]
    public virtual Modalidade Modalidade { get; set; }
}
  • If my solution meets your need, please mark it as an acceptable solution (green check) it improves my reputation ;)

  • What is the sense of separating classes and schedules?

  • From what I had understood at the same time there can be several Lessons. Ex: Mon. 08:00 - 09:00 - Judo. Mon. 08:00 - 09:00 - Karate. Mon. 08:00 - 09:00 - Zumba.

  • The problem is that this only allows the classes to have defined durations. If a class starts 9:30 and ends 11h?

  • Dai creates a Timetable for this and arrow that time in class.

  • Finally, what is the practical gain of this approach? What is added to the system having this separation?

  • The explanation is in my answer.

  • You don’t have to agree with the approach, it’s just an idea.

Show 3 more comments

Browser other questions tagged

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