PawelGerr / Thinktecture.EntityFrameworkCore

These libraries extend Entity Framework Core by a few features to make it easier to work with EF and for easier integration testing or to get more performance in some special cases.
https://dev.azure.com/pawelgerr/Thinktecture.EntityFrameworkCore
BSD 3-Clause "New" or "Revised" License
66 stars 17 forks source link

WithTableHints could not be translated #23

Closed CwjXFH closed 2 years ago

CwjXFH commented 2 years ago

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 2 years ago

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)

CwjXFH commented 2 years ago

@PawelGerr I got it, thanks!