Skip to content
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 Exists and ExistsAsync like Find and FindAsync #31203

Closed
nrhoffmann opened this issue Jul 6, 2023 · 3 comments
Closed

Add Exists and ExistsAsync like Find and FindAsync #31203

nrhoffmann opened this issue Jul 6, 2023 · 3 comments
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported

Comments

@nrhoffmann
Copy link

I am trying to check if an entity exists given a Type and key.

I'd like to have a method Exists and ExistsAsync that return a boolean. Currently, I use Find and FindAsync, but since I don't need the object returned from the database, its a waste, especially for big objects.

@roji
Copy link
Member

roji commented Jul 6, 2023

That should be trivial to implement in your project as an extension method over DbSet, which just does a LINQ query (e.g. context.Blogs.Any(b => b.Id == someId).

The reason Find/FindAsync exist as special methods is because they look inside EF's change tracker before actually contacting the database, so they save a database roundtrip if the entity instance is already tracked.

@nrhoffmann
Copy link
Author

How would you recommend getting a DbSet from typeof(TEntity)? I am using Find because I don't need to know the type or the key property name/s.

@ajcvickers
Copy link
Member

Note from triage: we will not make this change. Options are:

  • Implement specific, typed Exists methods for the entity types you have
  • Continue to use Find for this, and discard the allocated instances
  • Implement a non-type-specific Exists method by copying the code for Find. This could be made easier by Add key values sugar #7391.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2023
@ajcvickers ajcvickers added closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. and removed type-enhancement labels Jul 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants