WithTableHints could not be translated
CwjXFH opened this issue · 2 comments
CwjXFH commented
Hi,
When I use EFCore6 with the WithTableHints
method, will throw an exception:
System.InvalidOperationException: The LINQ expression DbSet<InfoEntity>()
.WithTableHints(
) could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Packages info
package | version |
---|---|
.NET | 6 |
Thinktecture.EntityFrameworkCore.SqlServer | 4.2.3 |
Microsoft.EntityFrameworkCore.SqlServer | 6.0.4 |
builder.Services.AddDbContext<InfoDbContext>(opt =>
{
opt.UseSqlServer(
"data source=local;initial catalog=Demo;user id=sa;password=passwd;App=Demo");
});
The API code:
[HttpGet]
public async Task<IActionResult> Query()
{
var info = await _dbContext.Infos
.WithTableHints(SqlServerTableHint.NoLock)
// .OrderBy(e => e.Id)
.FirstOrDefaultAsync(CancellationToken.None);
return Ok();
}
Thanks!
PawelGerr commented
Did you activate the feature?
.AddDbContext<DemoDbContext>(builder => builder
.UseSqlServer("conn-string",
options => options.AddTableHintSupport());
CwjXFH commented
@PawelGerr I got it, thanks!