Code First One to Many

Asked

Viewed 99 times

1

My setting:

 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.Core.Domain. 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.Core.Domain.Evaluationopergunta'.

I’ve tried to modelBuilder... And nothing. Why did you make a mistake?

1 answer

2


There are some things wrong. I will discuss them one by one here.

1. Note primary keys with [Key]

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

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

2. Foreign keys shall not be virtual

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

In addition, insert virtual a primitive can create a complicator for the Entity Framework. That is, try to list 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

Note that the error message says the following:

An Object of type 'System.Collections.Generic.List`1[[Application.Core.Domain.Evaluationopergunta, 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.Core.Domain.'.

You are assigning a list of the type IList<AvaliacaoPergunta> in a unitary 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, their 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; }
}
  • I found out that I was doing a very wrong deal... Thanks for the corrections Gypsy, besides Key, it’s good to add the Foreignkey, too?

  • ForeignKey does not need to. It is common to use this wrong note, and it radically changes the behavior of Model.

Browser other questions tagged

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