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

Query: Convert enum to underlying CLR type in type mapping when type … #25793

Merged
merged 1 commit into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/EFCore.Relational/Storage/RelationalTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,16 @@ public virtual DbParameter CreateParameter(
parameter.ParameterName = name;

value = ConvertUnderlyingEnumValueToEnum(value);

// We preserve convert nodes around enum to get actual enum value for type mapping
smitpatel marked this conversation as resolved.
Show resolved Hide resolved
// But when enum is cast manually to something same as implicit cast
// And type mapping is not really enum then we need to convert the enum value to current Clr Type
if (value?.GetType().IsEnum == true
&& ClrType.IsInteger())
{
value = Convert.ChangeType(value, ClrType);
}

if (Converter != null)
{
value = Converter.ConvertToProvider(value);
Expand Down
105 changes: 84 additions & 21 deletions test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,29 @@ public abstract class SimpleQueryTestBase : NonSharedModelTestBase
public virtual async Task Multiple_nested_reference_navigations(bool async)
{
var contextFactory = await InitializeAsync<Context24368>();
using var context = contextFactory.CreateContext();
var id = 1;
var staff = await context.Staff.FindAsync(3);

using (var context = contextFactory.CreateContext())
{
var id = 1;
var staff = await context.Staff.FindAsync(3);

Assert.Equal(1, staff.ManagerId);
Assert.Equal(1, staff.ManagerId);

var query = context.Appraisals
.Include(ap => ap.Staff).ThenInclude(s => s.Manager)
.Include(ap => ap.Staff).ThenInclude(s => s.SecondaryManager)
.Where(ap => ap.Id == id);
var query = context.Appraisals
.Include(ap => ap.Staff).ThenInclude(s => s.Manager)
.Include(ap => ap.Staff).ThenInclude(s => s.SecondaryManager)
.Where(ap => ap.Id == id);

var appraisal = async
? await query.SingleOrDefaultAsync()
: query.SingleOrDefault();
var appraisal = async
? await query.SingleOrDefaultAsync()
: query.SingleOrDefault();

Assert.Equal(1, staff.ManagerId);
Assert.Equal(1, staff.ManagerId);

Assert.NotNull(appraisal);
Assert.Same(staff, appraisal.Staff);
Assert.NotNull(appraisal.Staff.Manager);
Assert.Equal(1, appraisal.Staff.ManagerId);
Assert.NotNull(appraisal.Staff.SecondaryManager);
Assert.Equal(2, appraisal.Staff.SecondaryManagerId);
}
Assert.NotNull(appraisal);
Assert.Same(staff, appraisal.Staff);
Assert.NotNull(appraisal.Staff.Manager);
Assert.Equal(1, appraisal.Staff.ManagerId);
Assert.NotNull(appraisal.Staff.SecondaryManager);
Assert.Equal(2, appraisal.Staff.SecondaryManagerId);
}

protected class Context24368 : DbContext
Expand Down Expand Up @@ -123,5 +120,71 @@ protected class Staff
public int? SecondaryManagerId { get; set; }
public Staff SecondaryManager { get; set; }
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Comparing_enum_casted_to_byte_with_int_parameter(bool async)
{
var contextFactory = await InitializeAsync<Context21770>();
using var context = contextFactory.CreateContext();
var bitterTaste = Taste.Bitter;
var query = context.IceCreams.Where(i => i.Taste == (byte)bitterTaste);

var bitterIceCreams = async
? await query.ToListAsync()
: query.ToList();

Assert.Single(bitterIceCreams);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Comparing_enum_casted_to_byte_with_int_constant(bool async)
{
var contextFactory = await InitializeAsync<Context21770>();
using var context = contextFactory.CreateContext();
var query = context.IceCreams.Where(i => i.Taste == (byte)Taste.Bitter);

var bitterIceCreams = async
? await query.ToListAsync()
: query.ToList();

Assert.Single(bitterIceCreams);
}

protected class Context21770 : DbContext
{
public Context21770(DbContextOptions options)
: base(options)
{
}

public DbSet<IceCream> IceCreams { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IceCream>(
entity =>
{
entity.HasData(
new IceCream { IceCreamId = 1, Name = "Vanilla", Taste = (byte)Taste.Sweet },
new IceCream { IceCreamId = 2, Name = "Chocolate", Taste = (byte)Taste.Sweet },
new IceCream { IceCreamId = 3, Name = "Match", Taste = (byte)Taste.Bitter });
});
}
}

protected enum Taste : byte
{
Sweet = 0,
Bitter = 1,
}

protected class IceCream
{
public int IceCreamId { get; set; }
public string Name { get; set; }
public int Taste { get; set; }
}
}
}