diff --git a/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs b/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs index ca7f16a0c3c..981e7e8d2fd 100644 --- a/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs +++ b/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs @@ -694,6 +694,43 @@ protected class OwnedWithConverter public int Value { get; set; } } + [ConditionalFact] + public virtual void Id_object_as_entity_key() + { + using var context = CreateContext(); + var books = context.Set().Where(b => b.Id == new BookId(1)).ToList(); + + Assert.Equal("Book1", Assert.Single(books).Value); + } + + public class Book + { + public BookId Id { get; set; } + + public string Value { get; set; } + + public Book(BookId id) + { + Id = id; + } + } + + public class BookId + { + public readonly int Id; + + public BookId(int id) + { + Id = id; + } + + public override bool Equals(object obj) + => obj is BookId item && Id == item.Id; + + public override int GetHashCode() + => Id.GetHashCode(); + } + public abstract class CustomConvertersFixtureBase : BuiltInDataTypesFixtureBase { protected override string StoreName { get; } = "CustomConverters"; @@ -1172,6 +1209,17 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con new Parent { Id = 1 }, new Parent { Id = 2 }); }); + + modelBuilder.Entity( + b => + { + b.HasKey(e => e.Id); + b.Property(e => e.Id).HasConversion( + e => e.Id, + e => new BookId(e)); + + b.HasData(new Book(new BookId(1)) { Value = "Book1" }); + }); } private static class StringToDictionarySerializer diff --git a/test/EFCore.SqlServer.FunctionalTests/CustomConvertersSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/CustomConvertersSqlServerTest.cs index fa4f483a88c..2d4cc0b1723 100644 --- a/test/EFCore.SqlServer.FunctionalTests/CustomConvertersSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/CustomConvertersSqlServerTest.cs @@ -42,6 +42,8 @@ public virtual void Columns_have_expected_data_types() Blog.IsVisible ---> [nvarchar] [MaxLength = 1] Blog.RssUrl ---> [nullable nvarchar] [MaxLength = -1] Blog.Url ---> [nullable nvarchar] [MaxLength = -1] +Book.Id ---> [int] [Precision = 10 Scale = 0] +Book.Value ---> [nullable nvarchar] [MaxLength = -1] BuiltInDataTypes.Enum16 ---> [bigint] [Precision = 19 Scale = 0] BuiltInDataTypes.Enum32 ---> [bigint] [Precision = 19 Scale = 0] BuiltInDataTypes.Enum64 ---> [bigint] [Precision = 19 Scale = 0] @@ -264,6 +266,16 @@ public override void Object_to_string_conversion() // Return values are not string } + public override void Id_object_as_entity_key() + { + base.Id_object_as_entity_key(); + + AssertSql( + @"SELECT [b].[Id], [b].[Value] +FROM [Book] AS [b] +WHERE [b].[Id] = 1"); + } + private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected);