Skip to content

Commit 486047c

Browse files
authored
[automated] Merge branch 'release/9.0' => 'main'
2 parents 213d37b + 4b7c759 commit 486047c

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/EFCore.Cosmos/Extensions/Internal/PartitionKeyBuilderExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public static class PartitionKeyBuilderExtensions
2222
/// </summary>
2323
public static PartitionKeyBuilder Add(this PartitionKeyBuilder builder, object? value, IProperty? property)
2424
{
25+
if (value is not null && value.GetType() is var clrType && clrType.IsInteger() && property is not null)
26+
{
27+
var unwrappedType = property.ClrType.UnwrapNullableType();
28+
value = unwrappedType.IsEnum
29+
? Enum.ToObject(unwrappedType, value)
30+
: unwrappedType == typeof(char)
31+
? Convert.ChangeType(value, unwrappedType)
32+
: value;
33+
}
34+
2535
var converter = property?.GetTypeMapping().Converter;
2636
if (converter != null)
2737
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.Query;
5+
6+
#nullable disable
7+
8+
public class AdHocMiscellaneousQueryCosmosTest : NonSharedModelTestBase
9+
{
10+
#region 34911
11+
12+
[ConditionalFact]
13+
public virtual async Task Enum_partition_key()
14+
{
15+
var contextFactory = await InitializeAsync<Context34911>(
16+
onModelCreating: b => b.Entity<Context34911.Member>().HasPartitionKey(d => d.MemberType),
17+
seed: async context =>
18+
{
19+
context.Members.Add(new Context34911.Member { MemberType = Context34911.MemberType.Admin, Name = "Some Admin" });
20+
await context.SaveChangesAsync();
21+
});
22+
23+
await using (var context = contextFactory.CreateContext())
24+
{
25+
var admin = await context.Members.Where(p => p.MemberType == Context34911.MemberType.Admin).SingleAsync();
26+
Assert.Equal("Some Admin", admin.Name);
27+
}
28+
}
29+
30+
protected class Context34911(DbContextOptions options) : DbContext(options)
31+
{
32+
public DbSet<Member> Members { get; set; }
33+
34+
protected override void OnModelCreating(ModelBuilder modelBuilder)
35+
=> modelBuilder.Entity<Member>().HasData(new Member { Id = 1, Name = "Product 1" });
36+
37+
public class Member
38+
{
39+
public int Id { get; set; }
40+
public MemberType MemberType { get; set; }
41+
public string Name { get; set; }
42+
}
43+
44+
public enum MemberType
45+
{
46+
User,
47+
Admin
48+
}
49+
}
50+
51+
#endregion 34911
52+
53+
protected override string StoreName
54+
=> "AdHocMiscellaneousQueryTests";
55+
56+
protected override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
57+
=> builder.ConfigureWarnings(b => b.Ignore(CosmosEventId.NoPartitionKeyDefined));
58+
59+
protected override ITestStoreFactory TestStoreFactory
60+
=> CosmosTestStoreFactory.Instance;
61+
}

0 commit comments

Comments
 (0)