EF4. Add a object with relationship causes full table select
Ex 1:
"autor.ComentariosWorkItens.Add(comentarioWorkItem);"
autor.ComentariosWorkItens makes EF4 load all ComentariosWorkItens.
Ex 2:
comentarioWorkItem.Usuario = autor;
Fixup make EF load all ComentariosWorkItens too:
private void FixupUsuario(Usuario previousValue)
{
if (previousValue != null && previousValue.ComentariosWorkItens.Contains(this))
{
previousValue.ComentariosWorkItens.Remove(this);
}
if (Usuario != null)
{
if (!Usuario.ComentariosWorkItens.Contains(this))
{
Usuario.ComentariosWorkItens.Add(this);
}
}
}
开发者_运维问答
How can I prevent this?
1: Turn it around:
comentarioWorkItem.Usario = autor;
2: How is the EF supposed to answer this:
previousValue.ComentariosWorkItens.Contains(this)
... without looking into ComentariosWorkItens
?
I sent a email to Julie Lerman. Here her answer:
"I think this is known (and AWFUL) behavior on EF’s part. I’m not sure what to tell you. You might want to take a look in the MSDN forums to see if anyone from the team has anything to say about it. And, since I’m in the middle of reviewing my book before it goes to print, I will check to be sure I have a warning about this in there somewhere!"
精彩评论