Binding to ObjectQuery.Execute()

  • Thread starter Thread starter eager2008
  • Start date Start date
E

eager2008

Have a WPF ListView bound to the result of the execute method on a LINQ (to
Entities) query (ObjectQuery<T>), working fine the moment the ListView is
bound, but additions to the EntitySet does not show up in the ListView even
if the query itself holds the new entity.

The documentation says "...execute the query again using the Execute method.
This will bind the control to a new ObjectResult. You should do this to
ensure that object data is up-to-date..."

It seems like the control is not bound to the new ObjectResult and thus the
control is not updated, however if I set the DataContext of the ListView
"manually" (in code)again using the execute method it works, but this is not
the behaviour I expect by DataBinding.

What am I doing wrong ?
 
Good morning Eager2008. Welcome to Microsoft Newsgroup Support service. My
name is Jialiang Ge [MSFT]. It's my pleasure to work with you on this issue.

I think your quote of "...execute the query again using the Execute method.
"is from the MSDN article
http://msdn.microsoft.com/en-us/library/bb738469.aspx
<quote>
To ensure that the data source is up to date, you may need to execute the
query again using the Execute method. This will bind the control to a new
ObjectResult. You should do this to ensure that object data is up-to-date in
the following cases:
1. Changes are being made to the same ObjectContext outside the bound
control.
2. Changes are being made to data in the data source.
3. Objects were returned using the NoTracking option.
</quote>

According to the description, your expectation of the DataBinding behavior
seems that, the ListView should be *refreshed automatically (without any
coding)* when changes happen in the persistent store. You also found that
manually calling the code:

listViewCtrl.DataContext = query.Execute(MergeOption.OverwriteChanges);

can force the ListView to be update-to-date.

In fact, this depends on *how we made the additions to the EntitySet*:

(A)
If the additions are made in the three ways of the above quote, it is
necessary to manually call query.Execute again as the MSDN article
illustrates.
listViewCtrl.DataContext = query.Execute(MergeOption.OverwriteChanges);

You understood the reason rightly: the control does not know about the new
ObjectResult and thus the control is not updated

By default, our data model does not have callback when changes happen in the
data source, thus ListView does not know that the underlying data is
changed. There are some technologies, like Sql Dependency, that allows us to
register a callback and refresh the view in the client application by
calling Execute in the callback function.
http://msdn.microsoft.com/en-us/library/t9x04ed2.aspx
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldependency.aspx
(If you want more materials about SqlDependency, please let me know)

(B)
If the additions are made in the bound control, e.g. Edit/Insert is enabled
in the ListView and the users modify/add data to it, the control will be
refreshed automatically according to the entity set. We do not need to write
any code to refresh the UI.

Does the above answer your question? Please don't hesitate to tell me if you
have any other questions or concerns. I will do my best to help you.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks a lot for your answer, it partly answers my questions (or rather
confirms my suspicions).
SQL Dependency could perhaps solve the "problem" but it seems to involve
quite a bit of code to detect the actual change to the entity set.

Do you know why ObjectResult and EntitySet does not implement
INotifyCollectionChanged which would (I beleive) be the best approach (Guess
the ObjectQuery.Execute method shouls not return a new ObjectResult, but
somehow a modified one) ?
 
Hello Eager2008
Do you know why ObjectResult and EntitySet does not implement
INotifyCollectionChanged which would (I beleive) be the best approach
(Guess
the ObjectQuery.Execute method shouls not return a new ObjectResult, but
somehow a modified one) ?

This is a very good question. I will ask the product group for their design
considerations. Before I get their responses, I¡¯d like to share my opinion:

ObjectResult is the results of an object query against an Entity Data Model
and it implements IEnumerable. It does not implement the ICollection
interface for the Add/Remove methods, which means that ObjectResult can be
regarded as a ¡°ReadOnly¡± collection.

INotifyCollectionChanged is for setting up dynamic bindings so that
insertions or deletions in the collection update the UI automatically.
Because ObjectResult does not support Insertion/Deletion (it does not
implement ICollection interface), it becomes meaningless to implement
INotifyCollectionChanged.

So our question turns to be: why does ObjectResult not implement
ICollection?

I think it¡¯s possibly because the EDM team wants ObjectResult to be
light-weight. We can add/delete entities by calling
context.AddObject/DeleteObject, a centralized place for insertion/deletion
operations. The modification result is cached in the underlying
context.ObjectStateManager object. ObjectResult simply serves as a
enumerator of the query result. It makes each object¡¯s role more clear and
specific. Do you agree?

I will be back with the product team¡¯s explanation as soon as possible.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
Thanks for your answer and yes, I basically agree with your opinion regarding
the simplicity of the ObjectResult, but I think programmers would like very
much the possibility of simple dynamic binding somehow (perhaps the
ObjectResult could support ICollection and INotifyCollectionChanged
interfaces and have a "Refresh" method or similar which could be used instead
of ObjectQuery.Execute which returns a new instance of the ObjectResult))
 
Hello Eager2008

This is a very valuable point. Thank you! A good news is that, the product
group confirms they are considering implement INotifyCollectionChanged on
the EntityCollection properties in the future release, and they are also
very interested in your recommendation of implementing
INotifyCollectionChanged for query results. A relevant feedback was once
posted at:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=354250

In addition, the product group supplemented my answer for you question:

ObjectResult does, however, implement IListSource which through the
GetList() method provides you a potentially writable view of query results.
The IList that is returned implements IBindingList where you can call
Add/Delete as long as the IBindingList allows edits; this is determined by
whether our query returns entities in which case it is writable, or whether
the query returns projection results in which case the list is read-only.

Please feel at ease to let me know if you have any other questions or
concerns.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
Back
Top