Skip to content

Linq queries works with Ulid and throws with ExecuteSqlInterpolatedAsync #2259

@juchom

Description

@juchom

I'm using Ulid with EF Core and everything works fine when I use linq queries but the application throws an error when I use the ExecuteSqlInterpolatedAsync

I have this DbContext

public class ApplicationDbContext : DbContext
{
    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        configurationBuilder.Properties<Ulid>()
            .AreUnicode(false)
            .AreFixedLength(true)
            .HaveMaxLength(26)
            .HaveConversion<UlidToStringConverter>();
    }
}

The converter

internal class UlidToStringConverter : ValueConverter<Ulid, string>
{
    public UlidToStringConverter()
        : base(
            convertToProviderExpression: x => x.ToString(),
            convertFromProviderExpression: x => Ulid.Parse(x))
    {
    }
}

This query throws

        var id = Ulid.NewId();
        await ApplicationAdminDbContext.Database.ExecuteSqlInterpolatedAsync(@$"
UPDATE app.""Roles""
SET ""Name"" = {name}
WHERE ""Id"" = {id}");
Unhandled exception rendering component: The current provider doesn't have a store type mapping for properties of type 'Ulid'.
System.InvalidOperationException: The current provider doesn't have a store type mapping for properties of type 'Ulid'.

This query works

        var id = Ulid.NewId();
        var idForSql = id.ToString();

        await ApplicationAdminDbContext.Database.ExecuteSqlInterpolatedAsync(@$"
UPDATE app.""Roles""
SET ""Name"" = {name}
WHERE ""Id"" = {idForSql}");

Do you have any idea why is this happening and how should I solve that ?

Thanks a lot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions