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?
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?
– Diego Zanardo
ForeignKey
does not need to. It is common to use this wrong note, and it radically changes the behavior of Model.– Leonel Sanches da Silva