PawelGerr/Thinktecture.EntityFrameworkCore

WithTableHints could not be translated

CwjXFH opened this issue · 2 comments

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!

Did you activate the feature?

      .AddDbContext<DemoDbContext>(builder => builder
                               .UseSqlServer("conn-string", 
                                             options => options.AddTableHintSupport());

Docs: https://dev.azure.com/pawelgerr/Thinktecture.EntityFrameworkCore/_wiki/wikis/Thinktecture.EntityFrameworkCore.wiki/71/Table-Hints-(SQL-Server)

@PawelGerr I got it, thanks!