14

I have this problem where after a field (say Field3 in table MyTable) is updated on the database, MyTable.Field3 (in C#) is still returning the old value.

I suspect there is some caching...?

How do I force it to:
Read the value from the database?
OR
Update the value in the MyTable class?

Or is there anything I miss? I am new to LINQ

Thank you in advance.

Aximili
  • 26,401
  • 50
  • 141
  • 196

2 Answers2

13
DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, entity);

If you want to refresh your entire entity set, the easiest way is probably to just create a new DataContext and requery for everything.

Jake
  • 6,855
  • 6
  • 53
  • 64
  • 2
    It throws this exception: An object specified for refresh is not recognized. Apparently the record is changed using Context.SubmitChanges(); I can tell if a record is being updated, how do I refresh the value just for that record just after it is updated? – Aximili Apr 19 '10 at 07:13
  • Is there any sample code anywhere? I don't even know the terms I need to google, what should I search? Thank you – Aximili Apr 19 '10 at 07:16
  • 3
    When you call SubmitChanges(), your object should will refresh itself. If it does not, check the UpdateMode property on the fields in your Linq to SQL data model to make sure that it's set to "auto update". – Dave Markle Apr 20 '10 at 00:14
  • Thanks Dave, it may be because I was using different instances of DataContext. – Aximili Apr 20 '10 at 00:28
  • Make shure to call it with a second argument though ;) I just typed context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues); and that didn't work because I did not specify which entity to update. – citronas Jan 05 '12 at 13:20
6

You're misunderstanding the model. Linq to SQL doesn't pass through to the database each time you ask for a member. When you fetch an object from the database, it gets stored in memory as a Linq to SQL object, and there it stands. You don't get an up-to-date version of the object until you query the database again.

Dave Markle
  • 88,065
  • 20
  • 140
  • 165
  • 1
    I see, that's what I thought, thanks Dave. So how do I make it sync the value in the memory to match the one in the database? – Aximili Apr 19 '10 at 02:50
  • 1
    You would request the object again from the DataContext. – Robert Harvey Apr 19 '10 at 04:35
  • Alright I've actually been doing this wrong I have a lot of instances of DataContext everywhere. I'll fix that first. – Aximili Apr 20 '10 at 00:27
  • 1
    @Aximili, how do you request the object again from the DataContext, since Robert didn't answer on that, you might? :) I am facing same issue, my objects are stored in memory and they are not up to date each time i request them, so I have to use `DataServices.MyModel.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, "MODEL TO UPDATE" );` Is it good practice to use this refresh everytime I update and insert something to database? – Roxy'Pro Oct 03 '16 at 11:51
  • It's been a while I don't really remember. But I think you can either do that (which I don't think is a good practice as it can cause performance issue), or - I think I found it much more efficient to destroy the DataContext and reinitialize it. – Aximili Oct 06 '16 at 05:14