Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFact
_sqlExpressionFactory.Constant(true.ToString()));
}

if (instance.Type.IsEnum)
{
return _sqlExpressionFactory.Convert(instance, typeof(string));
}

return TypeMapping.TryGetValue(instance.Type, out var storeType)
? _sqlExpressionFactory.Function(
"CONVERT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public SqliteObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFactory
_sqlExpressionFactory.Constant(true.ToString()));
}

return TypeMapping.Contains(instance.Type)
return TypeMapping.Contains(instance.Type) || instance.Type.IsEnum
? _sqlExpressionFactory.Convert(instance, typeof(string))
: null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,20 @@ public override Task Where_compare_anonymous_types(bool async)

public override Task Subquery_inside_Take_argument(bool async)
=> Task.CompletedTask;

public override Task ToString_nullable_enum_property_projection(bool async)
=> AssertQuery(
async,
ss => ss.Set<Weapon>().Select(w => w.AmmunitionType.ToString()),
ss => ss.Set<Weapon>().Select(w => w.AmmunitionType != null ? w.AmmunitionType.ToString() : null));

public override Task ToString_nullable_enum_contains(bool async)
=> AssertQuery(
async,
ss => ss.Set<Weapon>().Where(w => w.AmmunitionType.ToString().Contains("Cart")).Select(g => g.Name));

public override Task ToString_enum_property_projection(bool async)
=> AssertQuery(
async,
ss => ss.Set<Gear>().Select(g => g.Rank.ToString()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
b.HasOne(w => w.Owner).WithMany(g => g.Weapons).HasForeignKey(w => w.OwnerFullName).HasPrincipalKey(g => g.FullName);
});

modelBuilder.Entity<Mission>().Property(m => m.Id).ValueGeneratedNever();
modelBuilder.Entity<Mission>(
b =>
{
b.Property(m => m.Id).ValueGeneratedNever();
b.Property(m => m.Difficulty).HasConversion<string>();
});

modelBuilder.Entity<SquadMission>(
b =>
Expand Down
41 changes: 31 additions & 10 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@ public virtual Task ToString_boolean_property_nullable(bool async)
async,
ss => ss.Set<LocustHorde>().Select(lh => lh.Eradicated.ToString()));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task ToString_enum_property_projection(bool async)
=> AssertQuery(
async,
ss => ss.Set<Gear>().Select(g => g.Rank.ToString()),
ss => ss.Set<Gear>().Select(g => ((int)g.Rank).ToString()));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task ToString_nullable_enum_property_projection(bool async)
=> AssertQuery(
async,
ss => ss.Set<Weapon>().Select(w => w.AmmunitionType.ToString()),
ss => ss.Set<Weapon>().Select(w => w.AmmunitionType != null ? ((int)w.AmmunitionType).ToString() : null));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task ToString_enum_contains(bool async)
=> AssertQuery(
async,
ss => ss.Set<Mission>().Where(g => g.Difficulty.ToString().Contains("Med")).Select(g => g.CodeName));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task ToString_nullable_enum_contains(bool async)
=> AssertQuery(
async,
ss => ss.Set<Weapon>().Where(w => w.AmmunitionType.ToString().Contains("1")).Select(g => g.Name),
ss => ss.Set<Weapon>().Where(w => w.AmmunitionType != null && ((int?)w.AmmunitionType).ToString().Contains("1")).Select(g => g.Name));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Include_multiple_one_to_one_and_one_to_many_self_reference(bool async)
Expand Down Expand Up @@ -3118,16 +3149,6 @@ public virtual Task Projecting_nullable_bool_in_conditional_works(bool async)
new { Prop = cg.Gear != null ? cg.Gear.HasSoulPatch : false }),
e => e.Prop);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Enum_ToString_is_client_eval(bool async)
=> AssertQuery(
async,
ss =>
ss.Set<Gear>().OrderBy(g => g.SquadId)
.ThenBy(g => g.Nickname)
.Select(g => g.Rank.ToString()));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Correlated_collections_naked_navigation_with_ToList(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public static IReadOnlyList<Mission> CreateMissions()
Timeline = new DateTimeOffset(599898024001234567, new TimeSpan(1, 30, 0)),
Duration = new TimeSpan(1, 2, 3),
Date = new DateOnly(2020, 1, 1),
Time = new TimeOnly(15, 30, 10)
Time = new TimeOnly(15, 30, 10),
Difficulty = MissionDifficulty.Low
},
new()
{
Expand All @@ -143,7 +144,8 @@ public static IReadOnlyList<Mission> CreateMissions()
Timeline = new DateTimeOffset(2, 3, 1, 8, 0, 0, new TimeSpan(-5, 0, 0)),
Duration = new TimeSpan(0, 1, 2, 3, 456),
Date = new DateOnly(1990, 11, 10),
Time = new TimeOnly(10, 15, 50, 500)
Time = new TimeOnly(10, 15, 50, 500),
Difficulty = MissionDifficulty.Medium
},
new()
{
Expand All @@ -153,7 +155,8 @@ public static IReadOnlyList<Mission> CreateMissions()
Timeline = new DateTimeOffset(10, 5, 3, 12, 0, 0, new TimeSpan()),
Duration = new TimeSpan(0, 1, 0, 15, 456),
Date = new DateOnly(1, 1, 1),
Time = new TimeOnly(0, 0, 0)
Time = new TimeOnly(0, 0, 0),
Difficulty = MissionDifficulty.Unknown
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Mission
public TimeSpan Duration { get; set; }
public DateOnly Date { get; set; }
public TimeOnly Time { get; set; }
public MissionDifficulty Difficulty { get; set; }

public virtual ICollection<SquadMission> ParticipatingSquads { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.TestModels.GearsOfWarModel;

public enum MissionDifficulty
{
Unknown = 0,
Low = 1,
Medium = 2,
High = 3,
Extreme = 4
}
Loading