views:

653

answers:

2

Hi Geeks,

I've a stroe procedure to update a record and after running it I use LinqToSql to delete the record. I know it is weird but I just want to test how smart the datacontext it is and understand how it works.

Since the datacontext caches the results so any change via it can be recorded but now I use a store procedure to update something, it would not know. So when I try to delete it, an exception comes out "Row not found or changed".

How I can tell the datacontext what I have updated? If I can do so the problem will sovle.

A: 

I guess I'm missing something, but if you know your stored proc just deletes the record, then why even bother with touching Linq2Sql after the fact? The DataContext isn't going to know about the deletion magically.

Nicholas H
Sorry, I didnt clarify my situation. My stored proc doesnt know what I'm gonna do next, but I know my sp has already updated something in the db, so after running the sp, I just wanna tell the datacontext I've updated something please sync my change.
What are you doing with the DataContext after the stored procedure has executed and deleted your record? Can you post more details please?
Nicholas H
One thing my sp will not delete the record, just updates something.And e.g. I use dc.SP_NAME to update something and then I may delete the record just updated for fun via LingToSql like thatvar item = dc.TableName.where(p=>p.Id == id).First();dc.TableName.DeleteOnSubmit(item);dc.SubmitChanges();As long as dc executes the submitchanges, an exception will be thrown "Row not found or changed". So I just wanna insert something between after running the sp and before getting the item out to notify the dc I've made such changes, plz update yourself 2
Because my dc is shared during a single web request, so I guess, I can create a new datacontext after running my sp and dispose the old datacontext. So next action or request will start using the new datacontext so the new datacontext knows the changes I've made, since it is after my changes
If you run your Linq "query" code:var item = dc.TableName.Where(p => p.Id == id).First()After you execute the SP, then you should be fine. If you are executing it before you run the SP, then yes, the DataContext has no idea about the change. You'd have to make a new DataContext or there is probably a method somewhere built into Linq2Sql to notify the DataContext to reload, but I unfortunately I don't know of it off the top of my head.
Nicholas H
My datacontext stores in HttpContext.Current.Items. Now I've a doubt that HttpContext.Current.Items only avaliable in a single request??? Since I tried to run the sp first and then I did another postback to run my LinqToSql Code but the same datacontext came out and the same error 2. Will HttpContext.Current.Items be disposed after the request ends? If so, I should get a new datacontext, right?
Prbably: DataContext x = new DataContext();x.Refresh();
Galilyou
A: 

You can use the DataContext Refresh() method passing in a System.Data.Linq.RefreshMode enumeration, here's an example:

NorthWindDataContext context = new NorthwindDataContext();
context.Refresh(RefreshMode.KeepChanges);

To Clarify:
The RefreshMode enumeration has 3 values:
1- KeepChanges
2- KeepCurrentValues 3- OverwriteCurrentValues

The version that used here (KeepChanges) forces the Refresh method to keep the modified value and update the other unmodified values by the value from the DB.

Galilyou