-
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 key values sugar #7391
Comments
@NickStrupat likes retrieving primary key values too... 👀 |
Hi @bricelam, @ajcvickers, The first two points are supported by my project EntityFramework.PrimaryKey, but the FK values is something I didn't think of. It sounds interesting and useful. How do you imagine the query clause building would look and/or work? |
@NickStrupat See https://github.com/aspnet/EntityFramework/blob/f9adcb64fdf668163377beb14251e67d17f60fa0/src/Microsoft.EntityFrameworkCore/Internal/EntityFinder.cs for where we do it internally. Not sure what the public API surface would look like. |
@ajcvickers can you provide a workaround showing how to call TryGetEntry ? Just like in EntityFinder.FindTracked, I want to return _stateManager.TryGetEntry(key, keyValues)?.Entity as TEntity, but I don't see how to get to there from the public interface. It could be either dbSet.TryFind or context.TryFind. sjb |
@sjb-sjb If you're asking about public API for this, then there isn't any. The only option using the public API would be to use DbSet.Local, possibly also keeping your own map if needed for perf. If you're willing to risk using internal APIs and are okay with reacting and updating when they break, then, try something like: var stateManager = context.GetService<IStateManager>();
var key = context.Model.FindEntityType(typeof(TEntity)).FindPrimaryKey();
var entity = stateManager.TryGetEntry(key, new object[] { myKeyValue })?.Entity; |
Thanks! |
Consider also creating a state manager index when a unique index (as opposed to an alternate key) is defined in the node. See #28263. |
It seems common to need to do things with key values--see #7308 for some examples.
Some things to consider:
Lookup an entity in the state manager using key values (the state manager part of Find)Lookup tracked entities by primary key, alternate key, or foreign key #29685The text was updated successfully, but these errors were encountered: