Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have used Linq to SQL for a long time, but now, because I need to work with MySQL I had to start with Entity FrameWork.
I combine ADO.NET and EF and I did not realize about this issue, but recently, I had found a very big problem that it`s driving me crazy.

A simple query...

Dim Query = (From p in db.products select p).ToList

It gives the results the first time good, but when I run this query again, it gives the SAME RESULTS again and again !!!

I mean, any change in the database it is not reflected in that query. The only way is to quit program and run again.

Visual Basic - VS2013
Entity Framework 5.0 (MySql)

Thanks.
Posted
Comments
Richard Deeming 11-Nov-15 16:44pm    
By "the same results", do you mean you don't see new records in the list, and deleted records still show?

Or do you mean that you don't see updates to the existing records that you've already loaded?
Member 11757010 11-Nov-15 17:41pm    
I mean I don't see updates made in the DB. (I have not try with new and deletes items yet)
Ex.
Run Query and get x results.
Change some data in DB.
Run again Query and get the same x results.

If I End the program and run again for the first time that Query I got the updated results.

It is like that Query fetch the data from db at first but then it obtains results from some cache or so.

You're not showing the entire ball of code, but what I think you're doing is creating a context instance when your app starts and then never destroying it until your app exists.

DO NOT DO THIS. Create a context instance when you need to query the data and then dispose the context when your done with in.

EF caches everything it sees from the database. If you request an entire table, the entity tracker loads every record in the table and rehydrates it in memory. All future requests for that data come from the tracker, not the database.

Your code should look more like this:
Dim result As IEnumerable(Of Product)
Using (context As MyDbContext = new MyDbContext())
    result = context.Products.ToList()
End Using
 
Share this answer
 
Comments
Member 11757010 12-Nov-15 10:52am    
That was it !!!

I used to do that way sometimes with Linq to SQL and no problem. But It seems that EF caches DB. Thanks!!!
Dave Kreskowiak 12-Nov-15 14:27pm    
Seriously, pickup the Entity Framework book by Julie Lerman(?) It's pretty much the definitive guide on EF.
EF caches your database.
Your class should implement IDisposable interface. Or you can define the scope for using the EF context.
VB
Using (db As MyDbEntities = new MyDbEntities())
    result = contedbxt.Products.ToList()
End Using

I'd suggesst you to use Repository Pattern whenever you're working with the EF.
Repository Pattern with Entity Framework using EntityTypeConfiguration[^]


-KR
 
Share this answer
 
Comments
Member 11757010 12-Nov-15 10:52am    
That was it

Thanks!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900