Investigate Hot Reload issues with EF Core
See original GitHub issueI’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");
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:
- Created 2 years ago
- Reactions:9
- Comments:18 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
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.