views:

32

answers:

1

Hello guys I have the following method:

var usuario;

            usuario = UniapontaService.GetUsuarioUniapontaPlanejamentoEstrategico(x => x.IdUsuario == VWUsuarioUniaponta.IdUsuario &&
                      x.PlanejamentoEstrategico.IdPlanejamentoEstrategico == HorarioTrabalhoCorrente.PlanejamentoEstrategico.IdPlanejamentoEstrategico);

            TxtTotalHorasMes.Text = usuario.QuantidadeHorasDisponivelMes.ToString();

            usuario = null;

When executes this method, and then executes it again it seem that the EF or something is caching the value of the first query: Exemple: go though the method, query for usuario that is 25, in that then i set null (that was a test) later on in the execution even changing that value in database, when it passes in this method again the variable is still 25....and when I save changes in another method it tries to save usuario from another method. This seems wrong because the variable scope is local and not global The Problem is, when the method is over it should not destroy that variable? How I can solve this??

+2  A: 

EF's ObjectContext tracks the objects that it loads. If you ask for the objects again, it gives you the same instance (not a copy) it gave you before.

http://msdn.microsoft.com/en-us/library/bb896269.aspx

The ObjectContext instance IS the scope of these instances. If you want to start a new UnitOfWork, you should start with a new ObjectContext.

David B
But in my case it's caching the variable and any value that I set does not work as expected, I saw in a article( http://bit.ly/9gG50s ) that once you query a value, the second search It go on the cache of the context instead going to the Database, how I can solve this??
Diego Correa
as david said you should create a new context or look at this http://stackoverflow.com/questions/2331225/how-to-refresh-objectcontext-cache-from-db
arch stanton