Entity Core, relationships

0

In my project I have two models: student and occurrence.

In the routine of registering / editing a student, it is quiet, however, in the routine of registering an occurrence, I do not know why, the entity saves nothing of the occurrence, but the complete student.

DbSet adds in the right context, but does not save the instance but the student. I am using the Fluent API to create the tables.

Could anyone help me?

OCCURRENCE

    public class Occurrence : Entity
{
    //Utilizado pelo Entity
    public Occurrence()
    {

    }

    public Occurrence(EOccurrenceType occurrenceType, string cause, string description, EOccurrenceStatus occurrenceStatus, DateTime date)
    {
        OccurrenceType = occurrenceType;
        Cause = cause;
        Description = description;
        OccurrenceStatus = occurrenceStatus;
        Date = date;

        AddNotifications(new Contract()
         .Requires()
         //Validações da data
         .IsLowerThan(Date.Date, DateTime.Now.AddDays(1), "Date", OccurrenceMessages.InvalidDate)
         //Validações da causa
         .HasMinLen(Cause, 1, "Cause", string.Format(SharedMessages.MinLength, "Causa", 1))
         .HasMaxLen(Cause, 255, "Cause", string.Format(SharedMessages.MaxLength, "Causa", 255))
         //Validações da descrição
         .HasMinLen(Description, 1, "Description", string.Format(SharedMessages.MinLength, "Descrição", 1))
         .HasMaxLen(Description, 255, "Description", string.Format(SharedMessages.MaxLength, "Descrição", 255))
         );
    }

    public EOccurrenceType OccurrenceType { get; private set; }
    public string Cause { get; private set; }
    public string Description { get; private set; }
    public EOccurrenceStatus OccurrenceStatus { get; private set; }
    public DateTime Date { get; set; }

    // Um aluno para várias ocorrências
    public Guid StudentId { get; private set; }
    public virtual Student Student { get; private set; }
    //relacionamento com usuário

    public void addStudent(Student student)
    {
        Student = student;
    }
}

STUDENT

 public class Student : Entity
{
    private IList<Occurrence> _occurrences;
    private IList<Parent> _parents;

    //Utilizado pelo Entity
    public Student()
    {
        _occurrences = new List<Occurrence>();
        _parents = new List<Parent>();
    }

    public Student(Name name, 
        Address address, 
        DateTime birthDate, 
        ETypeOfEducation eTypeOfEducation, 
        DateTime academicYear, 
        int serie, 
        string grade, 
        EShifts shifts, 
        int calledNumber, 
        string note)
    {
        Name = name;
        _parents = new List<Parent>();
        _occurrences = new List<Occurrence>();
        Address = address;
        BirthDate = birthDate;
        ETypeOfEducation = eTypeOfEducation;
        AcademicYear = academicYear;
        Serie = serie;
        Grade = grade;
        Shifts = shifts;
        CalledNumber = calledNumber;
        Note = note;

        AddNotifications(new Contract()
         .Requires()
         //Validações da serie
         .IsTrue(Serie > 0, "Serie", StudentMessages.InvalidSerie)
         //Validações da turma
         .IsNotNullOrEmpty(Grade, "Grade", StudentMessages.IinvalidGrade)
         .HasMaxLen(Grade, 1, "Grade", string.Format(SharedMessages.MaxLength, "Grade", 1))
         );
    }

    public Name Name { get; private set; }
    public Address Address { get; private set; }
    public DateTime BirthDate { get; private set; }
    //Verificar depois uma forma de implementar
    //public string Photo { get; set; }
    public ETypeOfEducation ETypeOfEducation { get; private set; }
    public DateTime AcademicYear { get; private set; }
    public int Serie { get; private set; }
    public string Grade { get; private set; }
    public EShifts Shifts { get; private set; }
    public int CalledNumber { get; private set; }
    public string Note { get; private set; }

    //Relacionamentos: 1 escola N alunos
    public School School { get; private set; }
    //Varias ocorrencias para um aluno
    public IReadOnlyCollection<Occurrence> Occurrences { get { return _occurrences.ToList(); }}
    // Varios parentes para um aluno
    public IReadOnlyCollection<Parent> Parents { get { return _parents.ToArray(); }}

    public void SetSchool(School school)
    {
        School = school;
    }

    public void AddOccurrences(Occurrence occurrence)
    {
        _occurrences.Add(occurrence);
    }

    public void AddParents(Parent parent)
    {
        _parents.Add(parent);
    }
}
    
asked by anonymous 03.05.2018 / 03:29

1 answer

1

How are you creating the objects?

Who do you create first?

How are you associating the occurrence with the student?

How are you saving the objects?

I believe that without this information it is difficult to help precisely, but maybe your problem is here:

public void AddOccurrences(Occurrence occurrence)
{
  _occurrences.Add(occurrence);
}

Where you place the occurrence in the list of occurrences, BUT DO NOT INFORM IT WHO IS THE STUDENT RESPONSIBLE FOR IT. Make the following change and see if it solves your problem.

public void AddOccurrences(Occurrence occurrence)
{
  // incluir a linha abaixo...
  occurrence.addStudent(this);
  _occurrences.Add(occurrence);
}

Doing the above way, your instance will be saved even if you only save the instance or if you decide to save the completed student.

context.Students.Update(student)
// ou context.Occurrences.Create(student)
context.SaveChanges()

I'm without an IDE now, so I'm not sure of the commands (Create and Update), but the idea is this, fill in the associations between the objects (Student knows Occurrence and vice versa)     

03.05.2018 / 04:59