Skip to content

Commit

Permalink
Query: Materialize keyless entity always if it is not coming from a l…
Browse files Browse the repository at this point in the history
…eft join

Resolves #19537
  • Loading branch information
smitpatel committed Mar 14, 2020
1 parent 6f6c1e9 commit 6a70528
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
62 changes: 38 additions & 24 deletions src/EFCore/Query/ShapedQueryCompilingExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,32 +401,46 @@ private Expression ProcessEntityShaper(EntityShaperExpression entityShaperExpres
}
else
{
expressions.Add(
Expression.IfThen(
primaryKey != null
? primaryKey.Properties.Select(
p =>
Expression.NotEqual(
_entityMaterializerSource.CreateReadValueExpression(
valueBufferExpression,
typeof(object),
p.GetIndex(),
p),
Expression.Constant(null)))
.Aggregate((a, b) => Expression.AndAlso(a, b))
: entityType.GetProperties()
.Select(
p =>
Expression.NotEqual(
_entityMaterializerSource.CreateReadValueExpression(
valueBufferExpression,
typeof(object),
p.GetIndex(),
p),
Expression.Constant(null)))
.Aggregate((a, b) => Expression.OrElse(a, b)),
if (primaryKey != null)
{

expressions.Add(Expression.IfThen(
primaryKey.Properties.Select(
p => Expression.NotEqual(
_entityMaterializerSource.CreateReadValueExpression(
valueBufferExpression, typeof(object), p.GetIndex(), p),
Expression.Constant(null)))
.Aggregate((a, b) => Expression.AndAlso(a, b)),
MaterializeEntity(
entityType, materializationContextVariable, concreteEntityTypeVariable, instanceVariable, null)));
}
else
{
if (entityShaperExpression.IsNullable)
{
expressions.Add(
Expression.IfThen(
entityShaperExpression.ConditionalProperties
.Select(
p =>
Expression.NotEqual(
_entityMaterializerSource.CreateReadValueExpression(
valueBufferExpression,
typeof(object),
p.GetIndex(),
p),
Expression.Constant(null)))
.Aggregate((a, b) => Expression.OrElse(a, b)),
MaterializeEntity(
entityType, materializationContextVariable, concreteEntityTypeVariable, instanceVariable, null)));
}
else
{
expressions.Add(
MaterializeEntity(
entityType, materializationContextVariable, concreteEntityTypeVariable, instanceVariable, null));
}
}
}

expressions.Add(instanceVariable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,19 @@ public virtual void FromSqlRaw_with_set_operation()
Assert.Equal(7, actual.Length);
}

[ConditionalFact]
public virtual void Keyless_entity_with_all_nulls()
{
using var context = CreateContext();

var actual = context.Set<OrderQuery>()
.FromSqlRaw(NormalizeDelimitersInRawString("SELECT NULL AS [CustomerID] FROM [Customers] WHERE [City] = 'Berlin'"))
.IgnoreQueryFilters()
.ToArray();

Assert.NotNull(Assert.Single(actual));
}

protected string NormalizeDelimitersInRawString(string sql)
=> Fixture.TestStore.NormalizeDelimitersInRawString(sql);

Expand Down

0 comments on commit 6a70528

Please sign in to comment.