views:

224

answers:

2

I have two tables (Knicks and Knacks) and a view that joins the two tables (PaddyWhacks). The tables have the following fields:

Knicks.key, Knicks.data1

Knacks.fkey, Knacks.data2

So the view contains:

PaddyWhacks.key, PaddyWhacks.data1, PaddyWhacks.data2

I'm using LINQ to SQL to retrieve the full view as IQueryable and everything looks good. After this I grab a row from Knicks and update it:

Knick k = db.Knicks.Single(row => row.data1 == 5);
k.data1 = 6;
db.SubmitChanges();

I know the update is correct, because if I perform a Count() on Knicks where data1 == 5, the result has changed (and I can peek inside the database and see the change). However, if I again retrieve Paddywhacks using:

IQueryable<PaddyWhack> rows = from row in db.PaddyWhacks select row;

The corresponding data1 value is still 5.

The application is running on a hosted web server and if I wait long enough and come back to try again, I'll see the updated value in the IQueryable view. I'm guessing something's being cached and maybe LINQ's not going all the way back to the database to get the data again. Is there a LINQ or SQL Server feature I need be aware of to resolve the problem?

+1  A: 

I don't use linqtosql, but you are correct that something is in cache. A view is just a "macro" query, the data is the data from the original tables, so it is always up to date. I would try to use a different function than IQueryable() or create a new connection and then run the query.

KM
But shouldn't there be some way to clear the cached information when I ask for the view the second time? I'm trying to move away from using the view entirely, but can't do that quite yet.
Neal S.
I ended up creating a new DataContext prior to the query. So far, this doesn't kill performance, but it's something I will keep an eye on.
Neal S.
+1  A: 

You could try to refresh the DataContext like this:

dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, rows);
edosoft
I guess I would want to call this just before issuing a LINQ query? Or would it be sufficient to call Refresh() after using the results from a query? That is, does Refresh() immediately update the data or does it just mark it for updating later?
Neal S.