views:

84

answers:

1

Hey,

I noticed that disposing and re-creating a DataContext is way faster than refreshing it with RefreshMode.OverwriteCurrentValues, which should effectively be the same.

While this causes my application to spike at 100% CPU for several seconds

dbc.Refresh(RefreshMode.OverwriteCurrentValues, dbc.Property);

the following code doesn't even eat up 50% of CPU and takes approximately a single second:

dbc.Dispose();
dbc = new PropertyManagementDataContext();

while also overwriting all my local changes and just updating my local data from the DB. Or did I overlook something?

Is there any danger to dispose and re-create the DataContext in order to get the latest data from the Database?

+1  A: 

Refresh() is actually round-tripping to the database. Disposing and recreating the context is using locally cached connection info. This will be faster.

Edit:
Also, according to the docs you're probably using Refresh() for the wrong reason. See the remark toward the bottom.

xanadont
I see, you're right.
Michael Barth