Modeling Models 1 to n

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 of the timetable to put the days of the week (sec, ter, quar, etc) and also the TeacherId, since a single modality can have different teachers, eg: Second, modality = Karate, room = 1 Teacher = 1, Fourth, Modality = Karate, room = 2, teacher = 2;

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

My question: When I first create the mode, I would give the database error, because the Schedule schedule needs the ModalityId, then create the schedule first (strange) I still would not even know which Id?

I tried to run and of course it gave error:

    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();
  

Can not convert an object of type 'Arena.Models.Modality'   no type   'System.Collections.Generic.ICollection'1 [Arena.Models.Modality]'.

    
asked by anonymous 22.09.2016 / 14:43

2 answers

3

@dorathoto seeing your idea of having an entity Horario , with start and end I believe your main problem is that Modalidade has ICollection of time and not the way you have mapped it. p>

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

Getting my example as follows

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; }
}

  

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

The way I mapped this would be contemplated. Of course it would take some checks to prevent the same room from being taken more than once in the same time slot. Same verification would have to be made for the teacher.

  

My question: When creating the mode first, it would give error in the Bank of   data, because the Time table needs the Id Mode, then create   first the schedule (strange) I still would not even know what the Id?

This question is part of the problem of your mapping, of having a list of times in the mode and not the Horario entity having a list of modalities.

    
22.09.2016 / 20:25
3

Would not it be the case to have the modeling below?

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

Where do they meet? In my opinion in the Classroom.

Each class is unique in the week and it has 1 Teacher, 1 schedule and 1 modality.

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 made the head code, did not test so it is possible that adjustments are needed, just to give you 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; }
}
    
22.09.2016 / 15:19