Skip to content

Commit

Permalink
[5.0.3] InMemory: Wrap composite key selector for owned type in Anony…
Browse files Browse the repository at this point in the history
…mousObject (#23732)

* InMemory: Wrap composite key selector for owned type in AnonymousObject

Resolves #23687

Composite key selector needs wrapper so that we compare them component-wise

* Update InMemoryQueryableMethodTranslatingExpressionVisitor.cs
  • Loading branch information
smitpatel authored Jan 13, 2021
1 parent 036b734 commit 39c48e0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1479,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 39c48e0

Please sign in to comment.