none
Entity's Navigation Peroperty is showing old deleted values , why ? RRS feed

  • Question

  • Hi,

    I have a self reference Person entity in my WPF entity Model. It has a navigation property Person1 , which actually shows all the child person objects to my parent Person object.

    Now everything is working fine when I get data.

    Lets say I have Person1->Person1-1->Person1-1-A , meaning Person1-1 has child Person1-1-A (grandchild). So now if I delete Person1-1-A from another instance of application or directly from DB , my original instance doesn't update the Navigation property of Person1-1 , and it still shows it has a child Person1-1-A.

    To get Person object from DB I m doing like this

     db.Refresh(System.Data.Objects.RefreshMode.StoreWins, db.tblPerson);

     return db.tblPerson;

    And EntityModel has generated my navigation property like this

            [XmlIgnoreAttribute()]
            [SoapIgnoreAttribute()]
            [DataMemberAttribute()]      
            [EdmRelationshipNavigationPropertyAttribute("MYDBModel", "FK_tblPerson_tblPerson1", "tblPerson1")]     
            public EntityCollection<tblPerson> tblPerson1 {}

     

    I have tried doing like this too  return db.tblPerson.Include("tblPerson1") to eager load my navigation peropety but it shows my old values, not the recent ones.

    How can I make sure I always get fresh Navigation Peroperty values from DB ???

    Thanks,

    Shah

    Thursday, September 29, 2011 4:07 PM

All replies

  • Any one plz ....!!!
    Thursday, September 29, 2011 6:14 PM
  • Hello,

    WPF Databinding is a machanism that provides a way to display and change the data between the UI and data source will be synchronized automatically. Check this article about more information Binding Object to Controls (EF4.1). http://msdn.microsoft.com/en-us/library/gg197521(v=VS.103).aspx

    If you have any question, I suggest you can send the demo application to us to analyze your question more real. My Mail: v-xugong@microsoft.com

    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, October 3, 2011 3:59 AM
  • Hi Larcolias,

    Thanks for your answer.

    My problem is different, Let me explain again.

     

    I have two instances of application running showing a tree like this           Root
                                                                                                                /   \
                                                                                                              N1    N2
                                                                                                                          \

                                                                                                                            N2-1

     

    Now this tree is build using a Navigation property of Person class (person class has Parent column and it contains ID of its parent from same Person table=>self referencing)

    If I delete N2-1 from Instance-1 of app, it deletes the node , update the DB and Navigation property is also shows 0 child against N2. If at the same time when I check tree on Instance-2 of app , it obviously shows the tree with node N2-1 , so I refresh it and it goes to DB and build the person collection againt... Now here is the problem when it gets the collection from DB , my N2s Navigation peroperty shows 1 child ..which shdoul't.

    So thats my question that why it is showing 1 child which does not exist in DB.... and I do this before quering to DB

    db.Refresh(System.Data.Objects.RefreshMode.StoreWins, db.tblPerson);

    so it should return me updated DB result.

    Now can you please help that how should I solve my problem.

     

    Thanks.

    Shah

    Monday, October 3, 2011 12:36 PM
  • ehmmm.... i think you first should check one thing of EDM. when you added a few new fields to database, you should "delete the connectionstring" >> "delete the model" >> "generate a new model" >> "rebuild solution". on your case, looks that you create a global instance of Context. you create it as local variable in order to reloading the latest data from database each time on different client end. in additional to update the latest data between differernce client ends, you should check the following things.

    1. save changes and managing concurrency. http://msdn.microsoft.com/en-us/library/bb738618.aspx

    2. sql notification http://www.dotnetspider.com/resources/2587-Get-All-files-under-given-folder-including-Sub-Directories-File.aspx

    Thanks,

    Werewolf,


    Just a newbie for everything.
    • Proposed as answer by Werewolfs Tuesday, October 4, 2011 8:38 AM
    Tuesday, October 4, 2011 8:38 AM
  • hi warewolfs,


    implementating SQL notification is out of scope in my case

    and to get any data from DB I frist call


    db.Refresh(System.Data.Objects.RefreshMode.StoreWins, db.MyEntity);

    which is also suggested in your provided MSDN article, but still I am getting old invalid data.........


    I was thinking that there will be some little configuration that I have missed but seems its not the case here...


    SO what should I do now , any more suggestions ?

    Tuesday, October 4, 2011 11:21 AM
  • RefreshMode.ClientWins replace the original values with the latest values from the database. ClientWins is the appropriate strategy for last-in-wins. Please check this article for more information. http://msdn.microsoft.com/en-us/magazine/cc700340.aspx

    HTH,


    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.

    Wednesday, October 5, 2011 4:44 AM
  • I have tried refreshing with ClientWins too , but its still showing old deleted values.

    Is there any way that I could specifically update/refresh the Navigation Properties only of an Entity ?

    Wednesday, October 5, 2011 1:12 PM
  • I have tried refreshing with ClientWins too , but its still showing old deleted values.

    Is there any way that I could specifically update/refresh the Navigation Properties only of an Entity ?


    No, short answer is to create a new context instance. Like "when did we know IE server was down?" Till we refresh our web page. When one deleted the data from the database, for other users, they have already loaded the context instance on their machines, they should refresh that context and load the updated data source. Ina ddition, you should double check my previous reply about SQL Notification machanism and use SqlDependency class to achieve refresh datasource at a regular time.

    Thanks,

    Werewolf,


    Just a newbie for everything.
    • Proposed as answer by Werewolfs Thursday, October 6, 2011 3:42 AM
    Thursday, October 6, 2011 3:42 AM
  • Warewolf, SQL Notification is out of scope in our case. 

    I cannot create new instance of context everytime when I have to query DB for my any entity.

    What I do is I have created a service per entity , and created a new instance of context in constructir of my services. So in short every entity has its own services and own instance of context.

    And i use that instance of context to refresh my data for person entity.

    The thing i cannot understand is that whynot it is showing me the data exist in DB instead of data in its cache !!!

    I belive it is very common scenarion in any large application to get the fresh data from DB instead to use the current already loaded data...

     

    Thursday, October 6, 2011 7:14 AM