none
Data is not updated when running two instances RRS feed

  • Question

  • when I run two instances of the same program that uses LTS, the data is not updated when I change some property in one instace and read back from the other instance.

    the datacontext is a singleton that lives the entire application run time and it is per instance.

    setting db.ObjectTrackingEnabled causes alot of exception  when updating the data. How can I  reslove this (prefferably without changing the datacontext from being singleton)


    calling      db.Refresh(RefreshMode.OverwriteCurrentValues);    does nothing.

    Friday, August 26, 2011 3:40 PM

Answers

All replies

  • Try this:

     

    http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/38c824c0-10ec-49cf-8d09-0b9119ca17d8/

     

     

    Friday, August 26, 2011 5:15 PM
  • The problem is that I have a set of records and I don't know which entity the other instance updated.  what if the other instace changed more than one record?
    Friday, August 26, 2011 5:57 PM
  • when I run two instances of the same program that uses LTS, the data is not updated when I change some property in one instace and read back from the other instance.

    Hello,

    Based on my understanding, you look like to save the entity state and check this information in other instances. I suggest you can check EntityState enumeration to save the state of an entity object. http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx and ObjectStateManager class http://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx

    Then, we should consider saving this state permanently. When we wanna serialize entity, EF only serializes entity data and not change tracking information, which includes the EntityState. So I suggest you have to create a second property, that is marked as DataMember. This will allow you to serialize this property. Then, you can share this value in the second instance.

    If I misunderstood, please feel free to let me know.

    Thanks,


    Larcolais Gong[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.

    Monday, August 29, 2011 2:11 PM
  • I should clarify: I have only one instace of the datacontext for the entire program lifetime (singelton) so when a second instace of the program updates some entity, for the first instace nothing has changed so the data is not read back. I guess after reading back the entities I can iterate over each one and call db.refresh(.. , entity)  but isn't this a major load on the server with many callback for each entity?
    Monday, August 29, 2011 2:35 PM
  • Hello again,

    I assumpted you fill the data into control such as DataGridView, memory data structure or others. AFAIK, the second instance have to refresh datasource manually. I made a test on my side. Hope this helps you.

        //Fill data into DataGridView control
        private void button5_Click(object sender, EventArgs e)
        {
          var query = (from p in datactx.Childrens
                 select p).ToList();
          this.dataGridView1.DataSource = query;
    
        }
    
        //Update the record which age equals 40
        private void button7_Click(object sender, EventArgs e)
        {
          var query = (from p in datactx.Childrens
                 where p.age == 40
                 select p).Single();
          query.name = "Larcolais";
          datactx.SubmitChanges();
        }
    
        //Refresh datasource and rebind it back to DataGridView
        private void button6_Click(object sender, EventArgs e)
        {
          datactx.Refresh(RefreshMode.OverwriteCurrentValues, datactx.Childrens);
          this.dataGridView1.DataSource = datactx.Childrens;
        }
    

    I found some similarities. Please check them.

    http://blogs.msdn.com/b/dinesh.kulkarni/archive/2008/05/23/linq-to-sql-tips-6-how-to-refresh-given-datacontext-won-t-stomp-on-your-objects.aspx

    http://stackoverflow.com/questions/3802765/datacontext-refresh-not-refreshing-the-object

    Thanks,


    Larcolais Gong[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.

    • Marked as answer by liorba Tuesday, August 30, 2011 12:31 PM
    Tuesday, August 30, 2011 7:38 AM
  • Hello again,

    I assumpted you fill the data into control such as DataGridView, memory data structure or others. AFAIK, the second instance have to refresh datasource manually. I made a test on my side. Hope this helps you.

      //Fill data into DataGridView control
      private void button5_Click(object sender, EventArgs e)
      {
       var query = (from p in datactx.Childrens
           select p).ToList();
       this.dataGridView1.DataSource = query;
    
      }
    
      //Update the record which age equals 40
      private void button7_Click(object sender, EventArgs e)
      {
       var query = (from p in datactx.Childrens
           where p.age == 40
           select p).Single();
       query.name = "Larcolais";
       datactx.SubmitChanges();
      }
    
      //Refresh datasource and rebind it back to DataGridView
      private void button6_Click(object sender, EventArgs e)
      {
       datactx.Refresh(RefreshMode.OverwriteCurrentValues, datactx.Childrens);
       this.dataGridView1.DataSource = datactx.Childrens;
      }
    

    I found some similarities. Please check them.

    http://blogs.msdn.com/b/dinesh.kulkarni/archive/2008/05/23/linq-to-sql-tips-6-how-to-refresh-given-datacontext-won-t-stomp-on-your-objects.aspx

    http://stackoverflow.com/questions/3802765/datacontext-refresh-not-refreshing-the-object

    Thanks,


    Larcolais Gong[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.

    Thanks a lot. it seems to work but isn't calling  refresh on the entire table is a major load on the system? or this is the exact way to refresh data with LTS and a connected mode?

    Why LTS doesn't do this automatically? what is the point of getting out of date data without te user knowing it? had I not check two instace I wouldn't have known the the data is "dirty".

    Tuesday, August 30, 2011 12:31 PM