none
LinqToSql datacontext return mistake reseult RRS feed

  • Question

  • i have partial class in my datacontext that have static method F1().

    when i first use from static method F1() it return right answer but when i change my table from SQLManagmentStudio or another program and call method F1()  it return last answer.

    i check SqlProfiler, datacontext send right Query to server but receive wrong answer in my application.

    when i renew datacontext (datacontextMyData dt=new datacontextMyData() ) it return true answer??

    how can i get true answer with out renew my datacontext?


    C# Is My World
    Saturday, May 8, 2010 3:30 PM

Answers

  • Use the DataContext.Refresh method ( http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.refresh.aspx ). For example:

    registeruser user = from usr in linqContext.registeredusers
              where usr.logon == "OmidTalebi"
              select usr).Single();
    
    linqContext.Refresh(RefreshMode.OverwriteCurrentValues, user);

    The DataContext is a cache; it holds information locally about records which it has already had dealings with. This is intended to optimise performance because it does not need to go to the database to get the record if it is being selected again. However, If another process modifies the data in the database, then your cache does not get updated, so you need to do the refresh.

    • Marked as answer by OmidTalebi Monday, May 10, 2010 5:43 AM
    Sunday, May 9, 2010 1:44 PM

All replies

  • Use the DataContext.Refresh method ( http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.refresh.aspx ). For example:

    registeruser user = from usr in linqContext.registeredusers
              where usr.logon == "OmidTalebi"
              select usr).Single();
    
    linqContext.Refresh(RefreshMode.OverwriteCurrentValues, user);

    The DataContext is a cache; it holds information locally about records which it has already had dealings with. This is intended to optimise performance because it does not need to go to the database to get the record if it is being selected again. However, If another process modifies the data in the database, then your cache does not get updated, so you need to do the refresh.

    • Marked as answer by OmidTalebi Monday, May 10, 2010 5:43 AM
    Sunday, May 9, 2010 1:44 PM
  • tank my friend.
    C# Is My World
    Monday, May 10, 2010 5:44 AM