Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in EF Core 9 (8 works) converting enum to string #35410

Closed
nbiada opened this issue Jan 4, 2025 · 3 comments
Closed

Error in EF Core 9 (8 works) converting enum to string #35410

nbiada opened this issue Jan 4, 2025 · 3 comments

Comments

@nbiada
Copy link

nbiada commented Jan 4, 2025

I have the following configuration for an entity:

public class OperaConfiguration: IEntityTypeConfiguration<Opera>
{
    public void Configure(EntityTypeBuilder<Opera> builder)
    {
        builder
            .ToTable("Opere")
            .Property(p => p.Stato)
            .HasConversion<string>();
    }
}

The entity has these fields (some removed for simplicity):

public class Opera
{
    public int Id { get; set; }
    public string Nome { get; set; } = string.Empty;
    public string? Descrizione { get; set; }
    public AttivoInattivoEnum Stato { get; set; } = AttivoInattivoEnum.Attivo;
}

This is the enumerator (Active/Inactive):

public enum AttivoInattivoEnum
{
    Attivo = 1,
    Inattivo = 2
}

This is the service retrieving the data with a DTO:

public class OpereService: CdaGenericService<Opera>, IOpereService
{
    public OpereService(IDbContextFactory<ApplicationDbContext> contextFactory) : base(contextFactory)
    {
    }

    public async Task<List<ListaOpereDto>> GetLista(Expression<Func<Opera, bool>>? filter = null)
    {
        await using var context = await _contextFactory.CreateDbContextAsync();
        var query = context.Opere
            .AsNoTracking();

        if (filter != null)
        {
            query = query.Where(filter);
        }

        var data = query
            .Select(s => new ListaOpereDto()
            {
                Id = s.Id,
                Nome = s.Nome,
                Descrizione = s.Descrizione ?? "",
                Attiva = s.Stato == AttivoInattivoEnum.Attivo
            });

        var x = data.ToQueryString();

        var result = await data.ToListAsync();

        return result;
    }
}

This is the DTO:

public class ListaOpereDto
{
    public int Id { get; set; }
    public string Nome { get; set; } = string.Empty;
    public string Descrizione { get; set; } = string.Empty;
    public bool Attiva { get; set; }
}

This is the SQL generated from EF Core version 8:

DECLARE @__ef_filter__p_0 int = 1;

SELECT [o].[Id], [o].[Nome], COALESCE([o].[Descrizione], N'') AS [Descrizione], CASE
    WHEN [o].[Stato] = N'Attivo' THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END AS [Attiva]
FROM [Opere] AS [o]
WHERE [o].[ClienteId] = @__ef_filter__p_0

This is the SQL generated from EF Core 9:

DECLARE @__ef_filter__p_0 int = 1;

SELECT [o].[Id], [o].[Nome], COALESCE([o].[Descrizione], N'') AS [Descrizione], 
    ~CAST([o].[Stato] ^ N'Attivo' AS bit) AS [Attiva]
FROM [Opere] AS [o]
WHERE [o].[ClienteId] = @__ef_filter__p_0

And this is the error:

Msg 402, Level 16, State 1, Line 4
The data types nvarchar(max) and nvarchar are incompatible in the '^' operator.
@nbiada nbiada changed the title Error in EF Core converting enum to string Error in EF Core 9 (8 works) converting enum to string Jan 4, 2025
@nbiada
Copy link
Author

nbiada commented Jan 4, 2025

The workaround is to add this property to the entity:

public bool Attiva => Stato == AttivoInattivoEnum.Attivo;

@ChrisJollyAU
Copy link
Contributor

Looks like duplicate of #35093 . Fixed in upcoming 9.0.1 in that case

@maumar
Copy link
Contributor

maumar commented Jan 5, 2025

I verified it works in patched 9 and produces:

SELECT [o].[Id], [o].[Nome], COALESCE([o].[Descrizione], N'') AS [Descrizione], CASE
    WHEN [o].[Stato] = N'Attivo' THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END AS [Attiva]
FROM [Opere] AS [o]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants