问题
In the msdn documentation there is a recommendation to not bind controls directly to an object query:
We recommend that you not bind controls directly to an ObjectQuery. Instead, bind controls to the result of the Execute method.
I've also read some answers here on SO that recommend it and also not to bind to instance of the model itself. Nevertheless I couldn't find why it is not recommended since all of the tests I have done so far seem to work fine.
Could anyone shed some light on the reasons why I shouldn't use these objects for my bindings?
回答1:
For the question "why shouldn't I bind controls to the ObjectQuery":
Quoted from here:
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.
If you do not call Execute
, what's displayed in your UI might be not up-to-date compared to what the query actually returns after being executed. The binding is, of course, not automatically updated when a change occurs in the database.
Second point:
We recommend that you not bind controls directly to an ObjectQuery. Instead, bind controls to the result of the Execute method. Binding in this manner prevents a query from being executed multiple times during binding.
回答2:
If you follow the link below that info, they give an explanation:
We recommend that you not bind controls directly to an ObjectQuery. Instead, bind controls to the result of the Execute method. Binding in this manner prevents a query from being executed multiple times during binding.
You do not want queries to be executed during binding. I wouldn't want a binding update trigger a database query without me knowing. Also, i've found that binding to entities directly causes other problems. By keeping the objects around, you also need to keep the ObjectContext around. Usually, you should keep the ObjectContext around as short as possible, preferrably in a using block.
来源:https://stackoverflow.com/questions/11345224/binding-to-entity-framework