'Can't resolve symbol "FirstOrDefaultAsync" in .Net Core Web API
I'm building a .Net Core Web API and in one of the files the FirstOrDefaultAsync() method just doesn't work although I'm using it in various files in the same project. I have all the necessary NuGet packages and namespaces. Other EFCore methods such as SaveChangesAsync seem to work just as usual.
using Microsoft.EntityFrameworkCore;
...
public async Task<NoteDetailsVm> Handle(GetNoteBodyQuery query, CancellationToken cancellationToken)
{
var entity = await _dbContext.FirstOrDefaultAsync(note => note.Id == query.Id, cancellationToken);
...
}
The IDE just doesn't recognize the method. Though the namespace gets highlited when I type in the name of the method, nothing really happens. In another file in the same project i have:
using Microsoft.EntityFrameworkCore;
...
public async Task<Unit> Handle(UpdateNoteCommand request, CancellationToken cancellationToken)
{
var entity = await _dbContext.Notes.FirstOrDefaultAsync(note =>
note.Id == request.Id, cancellationToken);
...
}
and everything works perfectly.
How can I make the IDE recognize the method?
Solution 1:[1]
you have to include a ef dbset name into db context code
var entity = await _dbContext.Notes.FirstOrDefaultAsync(note => note.Id == query.Id, cancellationToken);
//or
var entity = await _dbContext.Set<Note>().FirstOrDefaultAsync(note => note.Id == query.Id, cancellationToken);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Serge |
