Code First One to Many May be null

1

My scenery:

 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.Creat.Domination.AvalationQuestion,   Aplicacao.Core, Version = 1.0.0.0, Culture = neutral,   PublicKeyToken = null]] 'can not be set or removed from the Value   property of an EntityReference of type   'Application.Creat.Domination.AvalationQuestion'.

I have tried modelBuilder ... And nothing. Why this error?

    
asked by anonymous 06.12.2014 / 17:01

1 answer

2

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

1. Try to write down the primary keys with [Key]

We know that the Entity Framework is smart enough to deduce the Model keys, but still consider it important for the readability and organization of the Models layer to note the attribute that is the primary key for the table in the database.

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

2. Foreign keys should not be virtual

Unlike navigation properties, whose use of virtual is encouraged, it should not occur with properties that represent primitive values. The justification is that I do not see how necessary to derive a int , for example. For representation, int serves well for the purpose of representing the foreign key.

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

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. Note the error message

Notice that the error message says the following:

  

An object of type 'System.Collections.Generic.List'1 [[Application.Core.Domain.AvalationQuestion, Application.Core, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null]] 'can not be set or removed from the Value property of an EntityReference of type' Application.Core.Domain.AvalationQuestion '.

You are assigning a list of type IList<AvaliacaoPergunta> to 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 receiving 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; }
}
    
06.12.2014 / 19:20