Repository navigation
Expand file tree
/
Copy pathQueryRootTests.cs
More file actions
108 lines (80 loc) · 3.55 KB
/
Copy pathQueryRootTests.cs
File metadata and controls
108 lines (80 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.ComponentModel.DataAnnotations.Schema;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore.Projectables.FunctionalTests
{
public class QueryRootTests
{
public record Entity
{
public int Id { get; set; }
[Projectable(UseMemberBody = nameof(Computed2))]
public int Computed1 => Id;
private int Computed2 => Id * 2;
[Projectable(UseMemberBody = nameof(_ComputedWithBaking))]
[NotMapped]
public int ComputedWithBacking { get; set; }
private int _ComputedWithBaking => Id * 5;
}
[Fact]
public Task UseMemberPropertyQueryRootExpression()
{
using var dbContext = new SampleDbContext<Entity>(queryTrackingBehavior: QueryTrackingBehavior.NoTracking);
var query = dbContext.Set<Entity>();
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task DontUseMemberPropertyQueryRootExpression()
{
using var dbContext = new SampleDbContext<Entity>(queryTrackingBehavior: QueryTrackingBehavior.TrackAll);
var query = dbContext.Set<Entity>();
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task EntityRootSubqueryExpression()
{
using var dbContext = new SampleDbContext<Entity>();
var original = dbContext.Set<Entity>()
.Where(e => e.ComputedWithBacking == 5);
var query = original
.Select(e => new { Item = e, TotalCount = original.Count() });
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task AsTrackingQueryRootExpression()
{
using var dbContext = new SampleDbContext<Entity>(queryTrackingBehavior: QueryTrackingBehavior.NoTracking);
var query = dbContext.Set<Entity>().AsTracking();
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task AsNoTrackingQueryRootExpression()
{
using var dbContext = new SampleDbContext<Entity>(queryTrackingBehavior: QueryTrackingBehavior.TrackAll);
var query = dbContext.Set<Entity>().AsNoTracking();
return Verifier.Verify(query.ToQueryString());
}
// PopulateSettableProperties(false) skips the query root rewrite entirely, so a
// materialized entity keeps whatever its CLR member returns. Projectables are still
// expanded wherever a query reads them; see
// PopulateSettablePropertiesDisabledStillExpandsInProjection below.
[Fact]
public Task PopulateSettablePropertiesDisabledQueryRootExpression()
{
using var dbContext = new SampleDbContext<Entity>(
queryTrackingBehavior: QueryTrackingBehavior.NoTracking,
populateSettableProperties: false);
var query = dbContext.Set<Entity>();
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task PopulateSettablePropertiesDisabledStillExpandsInProjection()
{
using var dbContext = new SampleDbContext<Entity>(
queryTrackingBehavior: QueryTrackingBehavior.NoTracking,
populateSettableProperties: false);
var query = dbContext.Set<Entity>().Select(e => new { e.ComputedWithBacking });
return Verifier.Verify(query.ToQueryString());
}
}
}