How to use FirstOrDefaultAsync() in async await WEB API's
Solution 1
The EF Core async extension methods are defined in the Microsoft.EntityFrameworkCore namespace. This namespace must be imported for the methods to be available.
Solution 2
Got the solution just installed!
Install-Package Microsoft.EntityFrameworkCore.Tools in my .net core project and it start working... Thank you all guys for your comments.
Reason :
System.Linq namespace does support ToListAsync(), ToAsyncEnumerable(), and FindAsync() and SaveChangesAsync()
To work with FirstOrDefaultAsync() and SingleOrDefaultAsync() method we have to install this package.
Thanks again.
vijay sahu
I have almost 8 years of experience as a .NET Full Stack Developer. Below are the little glimpse of my experience. Current Project On: HTML, Angular 8 , .NET Core API , MS-SQL Server, C# , Entity-Framework , NUnit , Rally-JIRA, and GIT repository. Experience On: HTML , BootStrap , JavaScript , jQuery , ASP.NET MVC, ASP.NET API , WCF RestFull Service, MS-SQL Server, NServiceBus , Groovy Script , NUnit , Dependency Injection, Team-City for build , CI and CD , TFS, GIT , SVN. You can also visit my online portfolio at www.vijaysahu.in for brief details. Please feel free to connect my at any time.
Updated on June 22, 2022Comments
-
vijay sahu almost 2 years
I have created one .NET Core API , where my all methods are asynchronous but there is one requirement like
GetBalance()
which just return one entity (record) only.I am not able to using
SingleOrDefaultAsync()
, getting error like does not contain a definition for this.I am using simple basic EF Code First approach with no Repository pattern.
Here is my code example.
public async Task<ResponseBalanceModel> GetBalanceFor(int accountNumber) { var result = await _dbContext.Accounts.Where(x => x.AccountNumber == accountNumber) .SingleOrDefaultAsync(); // this is not working. /*Below tried code are not working. var result1 = await _dbContext.Accounts.Where(x => x.AccountNumber == accountNumber).SingleOrDefaultAsync(); var result2 = await _dbContext.Accounts.FirstOrDefaultAsync(x => x.AccountNumber == accountNumber); */ }
For more clarification
Reference of Entityframework in my project. (.NET Core)