Code First One to Many can be null

My scenario:

 public class AlunoAvaliacao
    {
        public int AlunoAvaliacaoID { get; set; }
        public DateTime Inicio { get; set; }
        public DateTime? Fim { get; set; }
        public virtual int AvaliacaoID { get; set; }
        public virtual Avaliacao Avaliacao { get; set; }
        public virtual int AlunoID { get; set; }
        public virtual Aluno Aluno { get; set; }
        public virtual ICollection<AlunoAvaliacaoPergunta> AlunoAvaliacaoPerguntas { get; set; }
    }

public class AlunoAvaliacaoPergunta
    {
        public int AlunoAvaliacaoPerguntaID { get; set; }
        public virtual int AlunoAvaliacaoID { get; set; }
        public virtual AlunoAvaliacao AlunoAvaliacao { get; set; }
        public virtual int AvaliacaoPerguntaID { get; set; }
        public virtual AvaliacaoPergunta AvaliacaoPergunta { get; set; }

        public string Resposta { get; set; }
        public bool Correta { get; set; }
    }

        var alunoAvaliacao = new AlunoAvaliacao();
        alunoAvaliacao.Aluno = aluno;
        alunoAvaliacao.Avaliacao = avaliacao;
        alunoAvaliacao.Inicio = DateTime.Now;
        alunoAvaliacao.AlunoAvaliacaoPerguntas = new List<AlunoAvaliacaoPergunta>();
        bdAlunoAvaliacao.Adicionar(alunoAvaliacao);
        bdAlunoAvaliacao.SalvarTodos();

An exception of type 'System.InvalidOperationException ' occurred in EntityFramework.dll but was not handled in user code

Additional information: an object of type 'System.Collections.Generic.List ' 1[[Application.Color.Domain.Assess the question, Application.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' cannot be set or removed from the Value property of an EntityReference of type 'Application.Color.Domain.Evaluate the question.

I've tried modelBuilder... And nothing. Why this mistake?

Author: Diego Zanardo, 2014-12-06

1 answers

There are some things wrong. I'll discuss one by one here.

1. Look for annotating the primary keys with [Key]

We know that the Entity Framework is quite smart to deduce the keys of the model, but I still consider it important for the readability and organization of the layer of models to note the attribute that is the primary key for the database table.

public class AlunoAvaliacao
{
    [Key]
    public int AlunoAvaliacaoID { get; set; }
    ...
}

2. Foreign keys should not be virtual

As opposed to navigation properties, the use of which virtual is encouraged, the same should not occur with properties representing primitive values. The justification is that I don't see it as necessary to derive a int, for example. For representation, int serves the purpose of representing the Foreign Key well.

In addition, inserting virtual into a primitive can create a complicator for the Entity Framework. That is, look for listing your foreign keys without virtual and just below the key primary:

public class AlunoAvaliacao
{
    [Key]
    public int AlunoAvaliacaoID { get; set; }
    public int AvaliacaoID { get; set; }
    public int AlunoID { get; set; }

    ...
}

public class AlunoAvaliacaoPergunta
{
    [Key]
    public int AlunoAvaliacaoPerguntaID { get; set; }
    public int AlunoAvaliacaoID { get; set; }
    public int AvaliacaoPerguntaID { get; set; }
    ...
}

3. Notice the error message

Notice that the error message reads as follows:

An object of type ' System.Collections.Generic.List ' 1 [[Application.Color.Domain.Evaluate The Question, Application.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' cannot be set or removed from the value property of an EntityReference of type 'Aplicacao.Color.Domain.Evaluate the question ' .

You are assigning a list of type IList<AvaliacaoPergunta> in a unit object, probably this:

public class AlunoAvaliacaoPergunta
{
    ...
    public virtual AvaliacaoPergunta AvaliacaoPergunta { get; set; }
    ...
}

The code placed in the question does not clarify what is happening, but somewhere, AvaliacaoPergunta is getting this IList<AvaliacaoPergunta>.

In the end, your models should look like this:

public class AlunoAvaliacao
{
    [Key]
    public int AlunoAvaliacaoID { get; set; }
    public int AvaliacaoID { get; set; }
    public int AlunoID { get; set; }

    public DateTime Inicio { get; set; }
    public DateTime? Fim { get; set; }

    public virtual Avaliacao Avaliacao { get; set; }
    public virtual Aluno Aluno { get; set; }

    public virtual ICollection<AlunoAvaliacaoPergunta> AlunoAvaliacaoPerguntas { get; set; }
}

public class AlunoAvaliacaoPergunta
{
    [Key]
    public int AlunoAvaliacaoPerguntaID { get; set; }
    public int AlunoAvaliacaoID { get; set; }
    public int AvaliacaoPerguntaID { get; set; }

    public string Resposta { get; set; }
    public bool Correta { get; set; }

    public virtual AlunoAvaliacao AlunoAvaliacao { get; set; }
    public virtual AvaliacaoPergunta AvaliacaoPergunta { get; set; }
}
 2
Author: Leonel Sanches da Silva, 2014-12-06 18:20:30