none
About ObjectQuery.Execute RRS feed

  • Question

  • Hi all,

    I want to ask about ObjectQuery.Execute method.

    Supposed I have this code :

    var cons = context.Contacts.Execute(MergeOption.AppendOnly);
    var entries = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added |
                   EntityState.Deleted | EntityState.Modified | EntityState.Unchanged);
    

    As I knew, ObjectQuery.Execute will force the Execution of ObjectQuery.

    On the code above, when It's executed, EF would send some Commands to Database,

    And cons variable filled with objects as results that comes from Database.

    But, why those objects had not attached to ObjectContext ? You can inspect the ObjectStateEntries for those objects (entries variabel will be empty).

    So, why we must set the MergeOption, whereas those objects not attached to ObjectContext finally ?

    And why those objects not attached to ObjectContext ?

    Could you show me how to use ObjectQuery.Execute method in the real-world apps. Because I'm confusing about its functionality.

     

    Monday, July 11, 2011 5:25 AM

Answers

  • Hi Agus,

    when we run the method var cons = context.Contacts.Execute(MergeOption.AppendOnly); it may return a DBDataRecord object for results and anonymous projections.

    When we run the mehod there is no entity created until we get it as First() or ToList().

    My sample:

    Add a event to the Objectmaterialized, it will be fired when the entities created.

    entities.objectmaterialized+= CustomeMaterialized;

    var cons = context.Contacts.Execute(MergeOption.AppendOnly); // no effect

    cons.ToList() ;// the method fired.

    Only when the Entity created it's will be attach to the ObjectContext.

    Hope that would be help.


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [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.

    Thursday, July 14, 2011 5:00 AM
    Moderator

All replies

  • Hi Agus,

     

    Welcome to the Agus.

    It's true that when we exute the ObjectQuery it's will load the datas from the database and add the objects to the context.

    Before you query the objects by GetObjectStateEntries you need to query the cons again.

    My sample:

    var cons = context.Contacts.Execute(MergeOption.AppendOnly);

     

    //Add

    var result=cons.ToList(); // then you can query the entities by GetObjectStateEntries.


    var entries = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added |
                   EntityState.Deleted | EntityState.Modified | EntityState.Unchanged);

     

    I hope that would be help you.


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [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.

    Tuesday, July 12, 2011 9:45 AM
    Moderator
  • So, my statement is that ObjectQuery.Execute doesn't push the objects to the ObjectContext.

    But it has gotten the datas from Database to memory. So, the datas is exist in memory.

    Then, when we explicitly call ToList, the objects that exist in memory will be pushed to the ObjectContext.


    Wednesday, July 13, 2011 12:07 PM
  • Hi Agus,

    when we run the method var cons = context.Contacts.Execute(MergeOption.AppendOnly); it may return a DBDataRecord object for results and anonymous projections.

    When we run the mehod there is no entity created until we get it as First() or ToList().

    My sample:

    Add a event to the Objectmaterialized, it will be fired when the entities created.

    entities.objectmaterialized+= CustomeMaterialized;

    var cons = context.Contacts.Execute(MergeOption.AppendOnly); // no effect

    cons.ToList() ;// the method fired.

    Only when the Entity created it's will be attach to the ObjectContext.

    Hope that would be help.


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [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.

    Thursday, July 14, 2011 5:00 AM
    Moderator
  • Now, I understand. Thanks David.
    Thursday, July 14, 2011 7:23 AM
  • You are welcome.


    David Peng [MSFT]

    Thursday, July 14, 2011 9:46 AM
    Moderator