ardalis / ardalis/SmartEnum

EF Core 7 InMemory - Throwing System.ArgumentException with any query involving a SmartEnum

Open
#400 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2.4k
Forks
184
PR merge metrics
No merged PRs in 30d

Description

Upgrading my solution to .NET 7 and suddenly hitting the following exception when running unit tests:

`System.ArgumentException : Expression of type 'Ardalis.SmartEnum.SmartEnum'2[Namespace.ReserveCostGroupEnum,System.String]' cannot be used for parameter of type 'System.String' of method 'Namespace.ReserveCostGroupEnum GetFromValue(System.String)' (Parameter 'arg0')`

Unsure if the problem lies with the Microsoft.EntityFrameworkCore.InMemory package or SmartEnum.
I am not getting the issue running through SQL server, only through unit tests using an InMemory db.
Tried applying the Conversion through:
```
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.ConfigureSmartEnum();
}
```
and

`builder.Property(p => p.ReserveCostGroup).HasColumnType("varchar(20)").HasConversion(p => p.Value, p => ReserveCostGroupEnum.FromValue(p));`

I've upgraded all the SmartEnum packages to the newest available.
An example of a simple query that fails:

`var enumTest = await dbContext.Test_Table.Where(q => q.ReserveCostGroup == ReserveCostGroupEnum.SubContractor).ToListAsync()`

an example of the SmartEnum:

```
public sealed class ReserveCostGroupEnum : SmartEnum
{
public static readonly ReserveCostGroupEnum None = new ReserveCostGroupEnum(nameof(None), "None");
public static readonly ReserveCostGroupEnum Buildings = new ReserveCostGroupEnum(nameof(Buildings), "Buildings");
public static readonly ReserveCostGroupEnum Contents = new ReserveCostGroupEnum(nameof(Contents), "Contents");
public static readonly ReserveCostGroupEnum Expenses = new ReserveCostGroupEnum(nameof(Expenses), "Expenses");
public static readonly ReserveCostGroupEnum SubContractor = new ReserveCostGroupEnum(nameof(SubContractor), "SubContractor");
public static readonly ReserveCostGroupEnum Costs = new ReserveCostGroupEnum(nameof(Costs), "Costs");
public ReserveCostGroupEnum(string name, string value) : base(name, value)
{
}
}
```

Any help would be greatly appreciated!

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the failing ReserveCostGroupEnum query against the EF Core 7 InMemory database and compare it with the SQL Server path. Inspect how ConfigureSmartEnum and the HasConversion expression are applied; done means the shown Where query runs without the ArgumentException while preserving SmartEnum value conversion.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.