Single, SingleOrDefault, First, FirstOrDefault in EF Core

In this article let us look at the Single, SingleOrDefault, First, FirstOrDefault methods in EF Core. These methods return a single record from the database based on a Where condition. We use these methods when the Where condition is not part of the Primary Key, But we want only one record in the query result. If the Where condition is part of the primary key then use the find method.

Database:
The Database for this tutorial is taken from the chinook database.
Source Code:
The source code of this project available in GitHub. It also contains the script of the database

Single or SingleOrDefault

We use Single or SingleOrDefault in EF Core, when we expect only a single row to exist in the table. If the query returns more than one record, then the system will throw an exception. If no records found in the database, then Single will throw an exception, while SingleOrDefault returns the default value i.e null.

Single

The following example covers all three scenarios for Single Method.

Single Record Exists

Single method querying for the Track Bohemian Rhapsody which exists in the database.

No Record Exists

Single method querying for the track Love Kills, which does not exist in the database. The code throws an Exception.

Multiple Record Exists

Single method querying for the tracks by the composer Mercury, Freddie. Since the query returns multiple records, It results in an exception.

SingleOrDefault

The SingleOrDefault returns the null record if there are no matching records in the database. In all other cases it behaves exactly same as Single Method.

Single Record Exists

Returns the record.

Best Entity Framework Core Books
The Best EF Core Books, which helps you to get started with EF Core  

No Record Exists

Returns null. Does not throw exception.

Multiple Record Exists

This results in an exception.

First or FirstOrDefault

Use First or FirstOrDefault when you expect more than one record in the database. This method returns the first matching row from the database. If no records found in the database, then First will throw an exception, while FirstOrDefault returns the default value which is null.

First

Single Record Exists

Returns the record.

No Record Exists

No Record exists. Throws an exception

Multiple Record Exists

Multiple Record exists. Returns the first record.

FirstOrDefault

The FirstOrDefault returns the null record if there are no matching records in the database. In all other cases it behaves exactly same as First Method.

Single Record Exists

Returns the record.

No Record Exists

Returns null. Does not throw exception

Multiple Record Exists

Returns the first record.

Difference between Single Vs SingleOrDefault Vs First Vs FirstOrDefault

The following tables show the difference between Single, SingleOrDefault, First & FirstOrDefault

ConditionSingleSingleOrDefaultFirstFirstOrDefault
No Matching Rows foundExceptionDefault ValueExceptionDefault Value
1 Matching row foundReturns The RowReturns The RowReturns RowReturns Row
More than 1 matching row foundExceptionExceptionReturns 1st RowReturns 1st Row

References

Summary

The Single, SingleOrDefault, First, FirstOrDefault in EF Core returns a single record. The methods starting with Single expects the database to contain only a one record else throws an exception. The Methods starring with First always returns the first row if it encounters more than one record. The methods ending with Default returns null if no records are found while others throws an exception.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top