none
When data is selected from database? RRS feed

  • Question

  • Hi

     

    I am not very familiar with LINQ to SQL but I read that the biggest difference between LINQ to SQL and LINQ to DataSet is that in LINQ to SQL we access database when we want access some table from DataContext class - each time.

     

    I made simple example. In DataContext object I have stored procedure that updates data in some table.

     

    This is my problem/question:

     

    1. I do foreach on my table from DataContext
    2. I call stored procedure
    3. I do again foreach on my table from DataContext but I do not see any changes.

    I checked database and data in database are updated. I have to call method DataContext.Refresh to have up to date data.

    Why I have to call this method to have fresh data?

    I read wrong article or I do something wrong?

     

     Regards


    kicaj
    Friday, January 21, 2011 9:48 PM

Answers

  • This is a common question. The data is stored in the application because of optimistic concurrency and performance, once the data is enumerated. If you want the latest data use refresh or create a new datacontext. I think this is two parts. First getting the data is delayed on queries until enumerated by foreach, tolist or something similar. Once that data is enumerated, that data is cached so that when you use the data again you have the same dataset.
    Saturday, January 22, 2011 4:45 AM

All replies

  • Hi

     

    I am not very familiar with LINQ to SQL but I read that the biggest difference between LINQ to SQL and LINQ to DataSet is that in LINQ to SQL we access database when we want access some table from DataContext class - each time.

     

    I made simple example. In DataContext object I have stored procedure that updates data in some table.

     

    This is my problem/question:

     

    1. I do foreach on my table from DataContext
    2. I call stored procedure
    3. I do again foreach on my table from DataContext but I do not see any changes.

    I checked database and data in database are updated. I have to call method DataContext.Refresh to have up to date data.

    Why I have to call this method to have fresh data?

    I read wrong article or I do something wrong?

     

     Regards


    kicaj
    Friday, January 21, 2011 11:24 PM
  • This is a common question. The data is stored in the application because of optimistic concurrency and performance, once the data is enumerated. If you want the latest data use refresh or create a new datacontext. I think this is two parts. First getting the data is delayed on queries until enumerated by foreach, tolist or something similar. Once that data is enumerated, that data is cached so that when you use the data again you have the same dataset.
    Saturday, January 22, 2011 4:45 AM
  • Hello Kicaj,

    Thanks for posting here.

    I found that you have posted a same thread in the LINQ To SQL Forum, so I will merge this one to that one to get better support.

    Thank you for your understanding and support!

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, January 22, 2011 5:55 AM
    Moderator
  • It does not explain another example.

     

    I do this:

     

    1. I Create instance of DataConext
    2. I do foreach on table A
    3. I change data in this table using  Management Studio
    4. I do next foreach on table A and I have fresh data. I did not have to call method DataContext.Refresh to have fresh data. Why?

     

    So I do not understand why in this example after second foreach I get up to date data.

    I work all the time of the same instance of DataContext.


    kicaj
    Saturday, January 22, 2011 8:40 AM