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?
– novic
is well founded, but then I thought of putting the Professor linked to the Schedule. ?
– Dorathoto
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.
– novic
I need a help.. rs Let’s try in my case above, the error pq?
– Dorathoto
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.
– novic
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.– Randrade
Now, creating a Model only for Schedules can cause you trouble, because editing the time of one will edit all the others.
– Randrade