Skip to content

Commit

Permalink
InMemory: Wrap composite key selector for owned type in AnonymousObject
Browse files Browse the repository at this point in the history
Resolves #23687

Composite key selector needs wrapper so that we compare them component-wise
  • Loading branch information
smitpatel committed Dec 18, 2020
1 parent 0e91fa1 commit 39b4ccc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ ProjectionBindingExpression projectionBindingExpression
.Select(p => p.ClrType)
.Any(t => t.IsNullableType());


var outerKey = entityShaperExpression.CreateKeyValuesExpression(
navigation.IsOnDependent
? foreignKey.Properties
Expand All @@ -1478,6 +1479,13 @@ ProjectionBindingExpression projectionBindingExpression
: foreignKey.Properties,
makeNullable);

if (foreignKey.Properties.Count > 1
&& !(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore23687", out var enabled) && enabled))
{
outerKey = Expression.New(AnonymousObject.AnonymousObjectCtor, outerKey);
innerKey = Expression.New(AnonymousObject.AnonymousObjectCtor, innerKey);
}

var outerKeySelector = Expression.Lambda(_expressionTranslator.Translate(outerKey), _queryExpression.CurrentParameter);
var innerKeySelector = Expression.Lambda(
_expressionTranslator.Translate(innerKey), innerQueryExpression.CurrentParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,63 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

#endregion

#region Issue23687

[ConditionalFact]
public virtual void Owned_reference_with_composite_key()
{
using (CreateScratch<MyContext23687>(Seed23687, "23687"))
{
using var context = new MyContext23687();

var query = context.Table.ToList();

var root = Assert.Single(query);
Assert.Equal("A", root.OwnedProp.A);
Assert.Equal("B", root.OwnedProp.B);
}
}

private static void Seed23687(MyContext23687 context)
{
context.Table.Add(new Root23687 { Id1 = 1, Id2 = 11, OwnedProp = new OwnedClass23687 { A = "A", B = "B" } });

context.SaveChanges();
}

[Owned]
public class OwnedClass23687
{
public string A { get; set; }
public string B { get; set; }
}

public class Root23687
{
public int Id1 { get; set; }
public int Id2 { get; set; }
public OwnedClass23687 OwnedProp { get; set; }
}

private class MyContext23687 : DbContext
{
public DbSet<Root23687> Table { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseInternalServiceProvider(InMemoryFixture.DefaultServiceProvider)
.UseInMemoryDatabase("23687");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Root23687>().HasKey(e => new { e.Id1, e.Id2 });
}
}

#endregion

#region SharedHelper

private static InMemoryTestStore CreateScratch<TContext>(Action<TContext> seed, string databaseName)
Expand Down

0 comments on commit 39b4ccc

Please sign in to comment.