Skip to content

Commit

Permalink
[release/8.0] Do extra processing for init-only fields (#32342) (#32355)
Browse files Browse the repository at this point in the history
Fixes #32310
  • Loading branch information
ajcvickers authored Nov 20, 2023
1 parent 6980d92 commit 3d0512a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public partial class RelationalShapedQueryCompilingExpressionVisitor
{
private sealed partial class ShaperProcessingExpressionVisitor : ExpressionVisitor
{
public static readonly bool UseOldBehavior32310 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32310", out var enabled32310) && enabled32310;

/// <summary>
/// Reading database values
/// </summary>
Expand Down Expand Up @@ -2063,7 +2066,10 @@ protected override Expression VisitBinary(BinaryExpression node)
OrElse(
ReferenceEqual(currentVariable, Constant(null)),
ReferenceEqual(parameter, Constant(null))),
MakeBinary(node.NodeType, node.Left, parameter),
!UseOldBehavior32310
&& node is { NodeType: ExpressionType.Assign, Left: MemberExpression leftMemberExpression }
? leftMemberExpression.Assign(parameter)
: MakeBinary(node.NodeType, node.Left, parameter),
Call(
PopulateListMethod.MakeGenericMethod(property.ClrType.TryGetElementType(typeof(IEnumerable<>))!),
parameter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,70 @@ public abstract class JsonQueryAdHocTestBase : NonSharedModelTestBase
protected override string StoreName
=> "JsonQueryAdHocTest";

#region 32310

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Contains_on_nested_collection_with_init_only_navigation(bool async)
{
var contextFactory = await InitializeAsync<MyContext32310>(seed: Seed32310);
await using var context = contextFactory.CreateContext();

var query = context.Pubs
.Where(u => u.Visits.DaysVisited.Contains(new DateOnly(2023, 1, 1)));

var result = async
? await query.FirstOrDefaultAsync()!
: query.FirstOrDefault()!;

Assert.Equal("FBI", result.Name);
Assert.Equal(new DateOnly(2023, 1, 1), result.Visits.DaysVisited.Single());
}

protected virtual void Seed32310(MyContext32310 context)
{
var user = new Pub32310
{
Name = "FBI",
Visits = new Visits32310
{
LocationTag = "tag",
DaysVisited = new List<DateOnly> { new(2023, 1, 1) }
}
};

context.Add(user);
context.SaveChanges();
}

protected class MyContext32310 : DbContext
{
public MyContext32310(DbContextOptions options)
: base(options)
{
}

public DbSet<Pub32310> Pubs => Set<Pub32310>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Pub32310>(b => { b.OwnsOne(e => e.Visits).ToJson(); });
}

public class Pub32310
{
public int Id { get; set; }
public required string Name { get; set; }
public Visits32310 Visits { get; set; } = null!;
}

public class Visits32310
{
public string LocationTag { get; set; }
public required List<DateOnly> DaysVisited { get; init; }
}

#endregion

#region 29219

[ConditionalTheory]
Expand Down Expand Up @@ -893,7 +957,7 @@ public class MyJsonEntityLazyLoadingProxies
}

#endregion

#region NotICollection

[ConditionalTheory]
Expand Down

0 comments on commit 3d0512a

Please sign in to comment.