开发者

About ObjectQuery.Execute

I want to ask about ObjectQuery.Execute method.

Supposed I have this code :

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

//entries will be empty
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 thos开发者_开发百科e 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.


Execute method returns ObjectResult and not ObjectQuery. So for the entities to be materialized, we have to stream the data that's in ObjectResult to any other collection type. We can also iterate over ObjectResult for the entities to be materialized and tracked by the context.

In your case, you can call ToList() and this would get all the entites materialized and change tracked. Because you are calling ToList() anyway, there is no need to call Execute().

var cons = context.Contacts.Execute(MergeOption.AppendOnly).ToList();
var entries = context.ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜