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]'.