question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Investigate Hot Reload issues with EF Core

See original GitHub issue

I’ve ported my project from v5 to v6 EF Core. Previously everything worked fine but now I’m getting random translation exceptions from EF.Functions.Like calls. I monitor the same operation and see the same data coming from input but sometimes it passes fine and sometimes throws an exception.

Behavior looks random, I can’t understand what’s causing it. But I see it is a cascade failure. Once one error pops, all queries with LIKE function starts to fail on execution: different tables, not a single one.

Call example:

public static async Task SetCacheLastAccess(string id, string type)
{
   await using var db = new DbContext();
   var entry = await db.Cache.FirstOrDefaultAsync(a => EF.Functions.Like(a.Id, id) && EF.Functions.Like(a.Type, type));
}

...
       public string Type { get; set; } = "-";
        public string Id { get; set; }
...

Database

            modelBuilder.Entity<ThdCacheEntry>().HasIndex(u => u.Id);

            modelBuilder.Entity<ThdCacheEntry>().ToTable("cache");

            modelBuilder.Entity<ThdCacheEntry>().Property(a => a.Id).HasColumnName("id");
            modelBuilder.Entity<ThdCacheEntry>().Property(a => a.Type).HasColumnName("type");
            modelBuilder.Entity<ThdCacheEntry>().Property(a => a.LastAccess).HasColumnName("lastAccess");
            modelBuilder.Entity<ThdCacheEntry>().Property(a => a.LastUpdate).HasColumnName("lastUpdate");
            modelBuilder.Entity<ThdCacheEntry>().Property(a => a.Content).HasColumnName("text");
            modelBuilder.Entity<ThdCacheEntry>().Property(a => a.Days).HasColumnName("days");

image

Exception details

The LINQ expression 'DbSet<ThdCacheEntry>()
    .Where(t => __Functions_0
        .Like(
            matchExpression: t.Id, 
            pattern: __id_1) && __Functions_0
        .Like(
            matchExpression: t.Type, 
            pattern: __type_2))' could not be translated. Additional information: Translation of method 'Microsoft.EntityFrameworkCore.DbFunctionsExtensions.Like' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information.
Translation of method 'Microsoft.EntityFrameworkCore.DbFunctionsExtensions.Like' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. 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.
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|15_0(ShapedQueryExpression translated, <>c__DisplayClass15_0& )
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, LambdaExpression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken)
   at ThunderED.DbHelper.<SetCacheLastAccess>d__35.MoveNext() in 

Include provider and version information

EF Core version: 6.0.0 Database provider: Microsoft.EntityFrameworkCore.Sqlite 6.0.0 Target framework: .NET 6.0 Operating system: WIn10 x64 21H1 IDE: Visual Studio 2022 17.0.2 Project: Blazor Server-Side

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:9
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
panthernetcommented, Dec 6, 2021

UPDATE: Okay not sure by 100% but looks like it happens after I use HOT RELOAD button in VS 2022 to apply changes OR reload happens automatically on code change. I use server-side Blazor for the project.

UPDATE: Confirmed. HOT RELOAD is the bad guy.

HOT RELOAD logs

THDWebServer (Web server): Hot reload session complete
THDWebServer (Web server): Hot reload session started
THDWebServer (Web server): Sending updates to running application
THDWebServer (Web server): Updates applied successfully
0reactions
rshekhtmcommented, Jun 5, 2023

Same issue with hot reload and EF.Functions.ILike usage. The hacky workaround I put in place was to catch the InvalidOperationException and invoke a query without ILike that is similar enough for development purposes, but is not suitable for production.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing the .NET Hot Reload experience for editing ...
Hot Reload works with many of your existing and upcoming project types such as WPF, Windows Forms, .NET MAUI previews, ASP.NET Core apps ......
Read more >
Hot reload with ASP.NET Core and TailwindCSS (or
I'm investigating a good hot-reload workflow for using TailwindCSS with ASP.NET. I've settled on using VSCode as VisualStudio doesn't have a ...
Read more >
Hot reload not working for asp.net MVC : RIDER-78204
After editing, the prompt to hot reload appears, but accepting it doesn't cause changes to automatically propagate, and reload the page doesn't work...
Read more >
Hot Reload is rubbish : r/Blazor
Another fun story, using Maui, tried using EF core sql lite, the tooling doesn't work with newer frameworks, so we have a project...
Read more >
Untitled
Ef core reload from database WebIf you use dotnet command line interface to execute EF Core commands then open command prompt and navigate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found