-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Find method on DbSet #797
Comments
Note for triage: I'm marking this for re-triage since I'm seeing more and more people asking about this and we haven't done any work on the mitigation we once talked about in the early days of EF7. |
Is FindAsync also in the consideration here as well or is that a separate issue? |
@jasoncavett You can consider |
I've seen some good discussion and feedback on this on the ASP.NET Insiders mailing list. We should re-discuss and maybe pull it into RTM. |
I would also like to add that I would like to see |
I am also going to add that it would be nice to have Find() in EF7 |
BTW, I have an answer on StackOverflow showing how you could write it yourself. |
That is awesome. I am having a hard time trying to find where IAccessor is located. Any ideas? |
@sirpadington In namespace |
@bricelam and @sirpadington do you find reference for IAccessor? I can not find on Microsoft.Data.Entity.Infrastructure? |
It was renamed to |
What is the progress on this "Add Find method on DbSet #797"? |
God we need this so much. |
Man, i was hoping we would get this in Update 2 👎 |
My question about implementing Find in EF7 is the efficiency. It's easy enough to take linear time to loop through the elements and find what you are looking for. But what I expect from Find is that it use the database indices to access the correct element (but without hitting the disk) in some reasonably fast manner like log n or better. If that is not what it does then I want a huge "use at your own risk" or disclaimer about the lack of efficiency in the documentation. Because, the reason I would look to Find in the first place is in the expectation that an in-memory search would be faster than a search to disk. So, while I obviously appreciate @bricelam's code suggestions on StackOverflow, I wouldn't want the final StackOverflow solution to be the basis of the EF7 implementation (unless that solution does something that I don't know about :-). |
Issue #797 Semantics are essentially the same as EF6: - If entity with given key is being tracked, then return it without a database query - Otherwise, query the database and track and return entity if found - Otherwise, return null
Issue #797 Semantics are essentially the same as EF6: - If entity with given key is being tracked, then return it without a database query - Otherwise, query the database and track and return entity if found - Otherwise, return null
Issue #797 Semantics are essentially the same as EF6: - If entity with given key is being tracked, then return it without a database query - Otherwise, query the database and track and return entity if found - Otherwise, return null
If this is done, how do I use it. I have 1.0.0 of EF7 and I don't see the find method.
|
@martinRocks the milestone is 1.1.0 |
You can try our nightly feeds, but they are unsupported and only really intended for trying out new features (not building production applications) https://www.myget.org/Gallery/aspnetcidev |
Quick question about the intended implementation, because a couple of people have posted the link to 'workarounds' on StackOverflow: will the final version maintain an identity map? Right now if you're in a loop, say, and do |
Yes, Find uses an identity map. Yes, find will return the new, unpersisted entity. |
I would like to reopen this discussion to ask for a version of Find that only looks in memory, and does not go to the database. The point is to use the EF indices to find the entry quickly, if it is in memory. My use case is this: I have a variety of views with viewmodels that can reference overlapping data i.e. the same entity loaded through several contexts. I want to be able to switch back and forth between the views and have them all up to date. I have no desire to implement my own identity resolution so that the different views/viewmodels share entities (that they each have loaded using different DbContexts). Neither do I want to reload everything in a given view/viewmodel just because a user edit in some other view/viewmodel has changed one entity. So it seems unavoidable that I will have several copies of a given entity in memory, coming from different DbSets. What I would like to do is, when one of the in-memory entities is changed in one DbSet then I would like to Find the other in-memory copies in the other DbSets (associated with other views / viewmodels) and update them appropriately. Obviously in this situation, having Find hit the database would be counterproductive. I realize that the philosophy is to dispose the context (and dbSets) after the unit of work is done -- perhaps right after the queries have finished. That doesn't make this problem go away, though. If you drop the context and the dbSets then it just entails that the duplicate entities are no longer associated with dbSets (which makes them even harder to Find :-) |
@ajcvickers thanks for this reference. Do you have any general / high level comment on the overall goal of synchronizing multiple copies of an entity, which arises due to loading the entity multiple times through different contexts? I think this problem is inevitable when you have multiple views of the data, but is it really? I would like to know if I've just gotten lost in the rabbit hole. What is the usual way to deal with this? All the best P.s. If this is in fact a standard problem (inter-context identity resolution would be one way to describe it) then it would be nice to have a standard way of dealing with it. |
@sjb-sjb Keep it constrained. That is, for an arbitrary number of instances, each of which can have any changes to both scalar properties or relationships, and for which there can be new entities, deleted entities, modified entities, and unchanged entities, it is very hard to define what the "correct" behavior is, and hard to come up with rules that will cover all cases. On the other hand, if there are two instances, one is known not to change, and the other only has scalar changes, then the task is pretty easy. So, as much as possible, constraint where and how changes can be made so as to keep your rules for how to merge changes as simple as possible, |
Thanks for the good advice. I think scalar properties and add / remove are enough. I plan to only change relationships by setting the reference Id, so that's a scalar property. Let's say I add a new child entity and set the parent id. I don't need to also explicitly create the relationship in the traversal collection, do I? Since the changes are being made on the ui thread and will be immediately propagated to the other entities, there is no way to have conflicting changes in two copies of an entity. What I realized is that one doesn't want to automatically replicate the changes made in one context in all the other contexts. Creating a relationship between two collections so that one of them picks up changes made in the other, should not be automatic but is an application decision. It should be part of the view model layer. Obviously MS doesn't have this kind of thing today, for example this goes beyond the intended design of the Template 10 validation / view model system. (ref @JerryNixon) Is there any plan to do something like this in the future? A comprehensive view model system including cross-context synchronization primitives (and validation) would be great. |
Microsoft.Data.Entity.Infrastructure namespace is not found ! |
@ajcvickers, a followup on the use of Find in replicating changes from one context to another. My current thinking is to constrain the problem (as you suggest) by only replicating changes when a SaveChanges occurs. That is a lot simpler than the general problem. One can just use Find and/or GetDatabaseValues directly. |
In the old world, we have Find method on Dbset that is missing in the new world. It would be nice to have this in new EF world too.
The text was updated successfully, but these errors were encountered: