What is the difference between Find, Single, SingleOrDefault, First and FirstOrDefault in EF Core?
Quick Answer
Find checks the change tracker first and only queries the DB on a miss (by primary key). Single/SingleOrDefault assert exactly one (or zero-or-one) match — they fetch up to 2 rows and throw if there's a second. First/FirstOrDefault just take the first match. The ...OrDefault variants return null instead of throwing on no match.
Find is special — it looks in the change tracker for an entity with that key and only goes to the database if it's not already loaded, so it can save a query. Single and SingleOrDefault mean “there should be exactly one”; they query TOP 2 and throw if a second row comes back, which is what you want when a duplicate would indicate a bug. First and FirstOrDefault just grab the first row and don't care if there are more. The OrDefault versions give you null rather than an exception when nothing matches. And First without an OrderBy is non-deterministic.
Findavoids a round-trip for an already-loaded entity; the others always hit the DB.- Use
Singlewhen “more than one” means a data-integrity bug you want surfaced; useFirstwhen duplicates are legitimate and you just want one. FirstwithoutOrderByreturns an arbitrary row — add an explicit order.SinglegeneratesSELECT TOP 2;FirstgeneratesSELECT TOP 1.