Bug #118460 NullReferenceException when querying an entity with StartsWith

Submitted: 16 Jun 2025 16:07 Modified: 27 Apr 16:11
Reporter: Manuel Zulian Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:9.0.3 OS:Linux
Assigned to: Assigned Account CPU Architecture:Any

[16 Jun 2025 16:07] Manuel Zulian
Description:
When querying an entity with StartsWith the following error is raised. Not sure about the severity of this one as I still could not find a workaround. Tried with a different mysql provider and it works. Works on version 8 of the framework/packages etc...

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Query.ExpressionExtensions.InferTypeMapping(SqlExpression[] expressions)
   at MySql.EntityFrameworkCore.Query.Internal.MySQLSqlExpressionFactory.ApplyTypeMappingOnCollate(MySQLCollateExpression collateExpression)
   at MySql.EntityFrameworkCore.Query.Internal.MySQLSqlExpressionFactory.ApplyNewTypeMapping(SqlExpression sqlExpression, RelationalTypeMapping typeMapping)
   at MySql.EntityFrameworkCore.Query.Internal.MySQLSqlExpressionFactory.ApplyTypeMapping(SqlExpression sqlExpression, RelationalTypeMapping typeMapping)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressionFactory.ApplyDefaultTypeMapping(SqlExpression sqlExpression)
   at MySql.EntityFrameworkCore.Query.Internal.MySQLSqlExpressionFactory.Collate(SqlExpression valueExpression, String charset, String collation)
   at MySql.EntityFrameworkCore.Query.Expressions.Internal.MySQLStringComparisonMethodTranslator.Utf8Bin(SqlExpression value)
   at MySql.EntityFrameworkCore.Query.Expressions.Internal.MySQLStringComparisonMethodTranslator.<>c__DisplayClass11_0.<MakeStartsWithExpression>b__0()
   at MySql.EntityFrameworkCore.Query.Expressions.Internal.MySQLStringComparisonMethodTranslator.CreateExpressionForCaseSensitivity(StringComparison cmp, Func`1 ifCaseSensitive, Func`1 ifCaseInsensitive)
   at MySql.EntityFrameworkCore.Query.Expressions.Internal.MySQLStringComparisonMethodTranslator.MakeStartsWithExpression(SqlExpression target, SqlExpression prefix, SqlExpression stringComparison)
   at MySql.EntityFrameworkCore.Query.Internal.MySQLStringMethodTranslator.Translate(SqlExpression instance, MethodInfo method, IReadOnlyList`1 arguments, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Query.RelationalMethodCallTranslatorProvider.<>c__DisplayClass7_0.<Translate>b__0(IMethodCallTranslator t)
   at System.Linq.Enumerable.IteratorSelectIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at Microsoft.EntityFrameworkCore.Query.RelationalMethodCallTranslatorProvider.Translate(IModel model, SqlExpression instance, MethodInfo method, IReadOnlyList`1 arguments, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateInternal(Expression expression, Boolean applyDefaultTypeMapping)
   at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression, Boolean applyDefaultTypeMapping)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateExpression(Expression expression, Boolean applyDefaultTypeMapping)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateLambdaExpression(ShapedQueryExpression shapedQueryExpression, LambdaExpression lambdaExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutorExpression[TResult](Expression query)
   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__DisplayClass11_0`1.<ExecuteCore>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Program.<Main>$(String[] args) in /home/manuel/RiderProjects/EFBug/EFBug/Program.cs:line 15

Example application provided later, running on a mysql container (version 9.1.0-1.el9).

Linux mint 22.1

dotnet 9.0.107

Connector version in the .csproj file at the end of the example

How to repeat:
using Microsoft.EntityFrameworkCore;

namespace EFBug;

public class Context : DbContext
{
    public Context(DbContextOptions<Context> options)
        : base(options)
    {
    }
    
    public DbSet<Image> Images { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Image>().ToTable("Image");

        modelBuilder.Entity<Image>().Property(e => e.Id)
            .HasColumnName("id")
            .HasColumnType("int(11)");

        modelBuilder.Entity<Image>().Property(e => e.CreatedAt)
            .HasColumnName("created_at")
            .HasColumnType("datetime");

        modelBuilder.Entity<Image>().Property(e => e.FileName)
            .IsRequired()
            .HasColumnName("file_name")
            .HasMaxLength(800)
            .HasColumnType("varchar(800)");

        modelBuilder.Entity<Image>().Property(e => e.FileUrl)
            .IsRequired()
            .HasColumnName("file_url")
            .HasMaxLength(8000)
            .HasColumnType("varchar(8000)");

        modelBuilder.Entity<Image>().Property(e => e.UpdatedAt)
            .HasColumnName("updated_at")
            .HasColumnType("datetime");
    }
}

====

namespace EFBug;

public class Image
{
    public Image()
    {
    }

    public int Id { get; set; }
    public string FileName { get; set; }
    public string FileUrl { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
}

===

// See https://aka.ms/new-console-template for more information

using EFBug;
using Microsoft.EntityFrameworkCore;

Console.WriteLine("Hello, World!");
var options = new DbContextOptionsBuilder<Context>()
    .UseMySQL("server=localhost;user=root;password=password;database=efbug;port=3306;")
    .Options;
var db = new Context(options);
db.Database.EnsureDeleted();
db.Database.EnsureCreated();

var images = db.Images.Where(i => i.FileName.StartsWith("test"));
Console.Write(images.Count());

===

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net9.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="MySql.EntityFrameworkCore" Version="9.0.3" />
    </ItemGroup>

</Project>
[6 Aug 2025 8:21] MySQL Verification Team
Hello Manuel Zulian,

Thank you for the detailed bug report.
Could you please try with latest version i.e Connector/NET 9.4.0 and let us know if you are facing any issues?

Regards,
Ashwini Patil
[6 Aug 2025 10:17] Manuel Zulian
Just tried the same example above with MySql.EntityFrameworkCore 9.0.6 (which pulls in mysq.data 9.4.0) and I get the same error.
[30 Sep 2025 3:11] liming ma
I can confirm the 10.0.0-preview also has the same problem.

https://www.nuget.org/packages/MySql.EntityFrameworkCore/10.0.0-preview
[22 Oct 2025 12:04] liming ma
I opened a PR to fix this problem.

See https://github.com/mysql/mysql-connector-net/pull/79
[22 Apr 9:41] Edward Gilmore
Posted by developer:
 
duplicate of 39041159.Closing.
[22 Apr 10:16] Manuel Zulian
In case anyone finds this bug, I just tried after updating to 9.7.0 and it still doesn't work. I have no idea where the duplicate is, I can't find it on this tracker, so no idea how to track the resolution of this.
[27 Apr 16:11] Manuel Zulian
This is still broken, the fix referenced in this issue fixed startsWith and endsWith but missed the contains method (see comment in https://github.com/mysql/mysql-connector-net/pull/79#issuecomment-4328540079)

Happy for someone to just apply the fix, I think opening another merge request for one line is overkill.
[29 Apr 9:05] Torben Hørup
1st: As Manuel Zulian, theres still problems with it

2nd: why didn't you also publish an updated MySql.EntityFrameworkCore together with Mysql.Data 9.7.0 ?

3rd: its difficult to see the changes - looks like your sync to github is broken

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.043 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.