Entity Core, in relationships

Asked

Viewed 67 times

0

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

In the routine of registering/editing a student, this quiet, however, in the routine of registering an occurrence, I do not know why, Entity does not save anything from the occurrence but the complete student.

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

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

PUPIL

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

1 answer

1


How you are creating the objects?

Who do you create first?

How you are associating the occurrence to 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 put the occurrence on the list of occurrences, BUT DO NOT INFORM HER 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 occurrence will be recorded even if you only save the occurrence or if you decide to save the full 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 the associations between the objects (Student knows Occurrence and vice versa)

  • 2

    Cool you want to help, but you used the response field to ask for clarification for the author of the question, even if you have put code, it is not certain that it will work, so the correct thing is to post a comment asking for clarification and then post a solution. You can post comments when you have a little more reputation points. Please use the answer field only to post a solution to the problem the question deals with.

  • 1

    Good morning, Noobsaibot, I see these limitations as a problem in Stackoverflow. I would even like to have made a comment asking for further clarification in order to offer a precise and definitive answer, but the system "does not allow". How shall I proceed now, then? I leave this my "answer" and edit when I’m sure of the code (I can check it today) or simply delete?

Browser other questions tagged

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