Skip to content

Commit b6998a3

Browse files
authored
Update to EF 9 (#817)
1 parent c4cf579 commit b6998a3

File tree

13 files changed

+103
-86
lines changed

13 files changed

+103
-86
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;CS0649;CS8632;EF1001</NoWarn>
5-
<Version>12.4.0</Version>
5+
<Version>13.0.0</Version>
66
<LangVersion>preview</LangVersion>
77
<AssemblyVersion>1.0.0</AssemblyVersion>
88
<PackageTags>EntityFramework, Verify</PackageTags>

src/Directory.Packages.props

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageVersion Include="EfClassicLocalDb" Version="19.2.1" />
8-
<PackageVersion Include="EfLocalDb" Version="19.2.1" />
7+
<PackageVersion Include="EfClassicLocalDb" Version="20.0.0" />
8+
<PackageVersion Include="EfLocalDb" Version="20.0.0" />
99
<PackageVersion Include="EntityFramework" Version="6.5.1" />
1010
<PackageVersion Include="MarkdownSnippets.MsBuild" Version="27.0.2" />
11-
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.11" />
12-
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.11" />
13-
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.11" />
14-
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.11" />
15-
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.11" />
16-
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.11" />
11+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
12+
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
13+
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
14+
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.0" />
15+
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
16+
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
1717
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
1818
<PackageVersion Include="NUnit" Version="4.2.2" />
1919
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0" />
2020
<PackageVersion Include="Polyfill" Version="7.5.0" />
2121
<PackageVersion Include="ProjectDefaults" Version="1.0.144" />
2222
<PackageVersion Include="System.Data.SqlClient" Version="4.9.0" />
23-
<PackageVersion Include="Verify" Version="28.2.1" />
23+
<PackageVersion Include="Verify" Version="28.3.2" />
2424
<PackageVersion Include="Verify.DiffPlex" Version="3.1.2" />
25-
<PackageVersion Include="Verify.NUnit" Version="28.2.1" />
25+
<PackageVersion Include="Verify.NUnit" Version="28.3.2" />
2626
<PackageVersion Include="Verify.SqlServer" Version="10.1.0" />
2727
</ItemGroup>
2828
</Project>

src/Verify.EntityFramework.Tests/GlobalUsings.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@
77
global using Microsoft.Data.Sqlite;
88
global using Microsoft.EntityFrameworkCore;
99
global using Microsoft.Extensions.DependencyInjection;
10-
global using Microsoft.Extensions.Hosting;
11-
global using VerifyTests.EntityFramework;
10+
global using Microsoft.Extensions.Hosting;

src/Verify.EntityFramework.Tests/Snippets/DataContext/SampleDbContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
public DbSet<Employee> Employees { get; set; } = null!;
55
public DbSet<Company> Companies { get; set; } = null!;
66

7-
protected override void OnModelCreating(ModelBuilder builder)
7+
protected override void OnModelCreating(ModelBuilder model)
88
{
9-
builder
9+
model
1010
.Entity<Company>()
1111
.HasMany(_ => _.Employees)
1212
.WithOne(_ => _.Company)
1313
.IsRequired();
14-
builder.Entity<Employee>();
14+
model.Entity<Employee>();
1515
}
1616
}

src/Verify.EntityFramework/Extensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ public static IQueryable<object> AsNoTracking(this IQueryable<object> set, Type
1111
return (IQueryable<object>) genericNoTracking.Invoke(null, [set])!;
1212
}
1313

14+
public static string? NameOrAlias(this TableExpressionBase tableExpressionBase)
15+
{
16+
if (tableExpressionBase.Alias != null)
17+
{
18+
return tableExpressionBase.Alias;
19+
}
20+
21+
if (tableExpressionBase is TableExpression tableExpression)
22+
{
23+
return tableExpression.Name;
24+
}
25+
26+
return null;
27+
}
28+
1429
public static IQueryable<object> Set(this DbContext data, Type t) =>
1530
(IQueryable<object>) setMethod
1631
.MakeGenericMethod(t)

src/Verify.EntityFramework/LogCommandInterceptor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public override ValueTask<int> NonQueryExecutedAsync(DbCommand command, CommandE
4949
void Add(string type, DbCommand command, CommandEndEventData data, Exception? exception = null)
5050
{
5151
var context = data.Context;
52-
if (context != null && context.IsRecordingDisabled())
52+
if (context != null &&
53+
context.IsRecordingDisabled())
5354
{
5455
return;
5556
}
Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
sealed class MissingOrderByVisitor : ExpressionVisitor
1+
sealed class MissingOrderByVisitor :
2+
ExpressionVisitor
23
{
3-
List<OrderingExpression> orderedExpressions = [];
4+
List<TableExpressionBase> orderedTables = [];
45

56
public override Expression? Visit(Expression? expression)
67
{
@@ -11,41 +12,38 @@
1112

1213
switch (expression)
1314
{
14-
case ShapedQueryExpression shapedQueryExpression:
15-
if(shapedQueryExpression.ResultCardinality != ResultCardinality.Enumerable)
15+
case ShapedQueryExpression shaped:
16+
if(shaped.ResultCardinality != ResultCardinality.Enumerable)
1617
{
1718
return null;
1819
}
19-
Visit(shapedQueryExpression.QueryExpression);
20-
return shapedQueryExpression;
20+
Visit(shaped.QueryExpression);
21+
return shaped;
2122

22-
case RelationalSplitCollectionShaperExpression splitExpression:
23-
foreach (var table in splitExpression.SelectExpression.Tables)
23+
case RelationalSplitCollectionShaperExpression split:
24+
foreach (var table in split.SelectExpression.Tables)
2425
{
2526
Visit(table);
2627
}
2728

28-
Visit(splitExpression.InnerShaper);
29+
Visit(split.InnerShaper);
2930

30-
return splitExpression;
31+
return split;
3132

3233
case TableExpression tableExpression:
3334
{
34-
foreach (var orderedExpression in orderedExpressions)
35+
foreach (var table in orderedTables)
3536
{
36-
if (orderedExpression.Expression is ColumnExpression columnExpression)
37+
if (table == tableExpression)
3738
{
38-
if (columnExpression.TableAlias == tableExpression.Name)
39-
{
40-
return base.Visit(expression);
41-
}
39+
return base.Visit(expression);
40+
}
4241

43-
if (columnExpression.Table is PredicateJoinExpressionBase joinExpression)
42+
if (table is PredicateJoinExpressionBase join)
43+
{
44+
if (join.Table == tableExpression)
4445
{
45-
if (joinExpression.Table == tableExpression)
46-
{
47-
return base.Visit(expression);
48-
}
46+
return base.Visit(expression);
4947
}
5048
}
5149
}
@@ -57,22 +55,32 @@ TableExpression must have at least one ordering.
5755
{ExpressionPrinter.Print(tableExpression)}
5856
""");
5957
}
60-
case SelectExpression selectExpression:
58+
case SelectExpression select:
6159
{
62-
var orderings = selectExpression.Orderings;
60+
var orderings = select.Orderings;
6361
if (orderings.Count == 0)
6462
{
6563
throw new(
6664
$"""
6765
SelectExpression must have at least one ordering.
6866
Expression:
69-
{PrintShortSql(selectExpression)}
67+
{PrintShortSql(select)}
7068
""");
7169
}
7270

7371
foreach (var ordering in orderings)
7472
{
75-
orderedExpressions.Add(ordering);
73+
if (ordering.Expression is not ColumnExpression column)
74+
{
75+
continue;
76+
}
77+
78+
if (!TryFindTable(select.Tables, column.TableAlias, out var table))
79+
{
80+
continue;
81+
}
82+
83+
orderedTables.Add(table);
7684
}
7785

7886
return base.Visit(expression);
@@ -86,6 +94,28 @@ SelectExpression must have at least one ordering.
8694
}
8795
}
8896

97+
static bool TryFindTable(IReadOnlyList<TableExpressionBase> tables, string name, [NotNullWhen(true)] out TableExpressionBase? result)
98+
{
99+
foreach (var table in tables)
100+
{
101+
if (table.NameOrAlias() == name)
102+
{
103+
result = table;
104+
return true;
105+
}
106+
107+
if (table is LeftJoinExpression join &&
108+
join.Table.NameOrAlias() == name)
109+
{
110+
result = join;
111+
return true;
112+
}
113+
}
114+
115+
result = null;
116+
return false;
117+
}
118+
89119
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "PrintShortSql")]
90120
static extern string PrintShortSql(SelectExpression expression);
91121
}

src/Verify.EntityFramework/MissingOrder/RelationalFactory.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
class RelationalFactory : RelationalShapedQueryCompilingExpressionVisitorFactory
1+
class RelationalFactory(
2+
ShapedQueryCompilingExpressionVisitorDependencies dependencies,
3+
RelationalShapedQueryCompilingExpressionVisitorDependencies relational) :
4+
RelationalShapedQueryCompilingExpressionVisitorFactory(dependencies, relational)
25
{
3-
public RelationalFactory(ShapedQueryCompilingExpressionVisitorDependencies dependencies, RelationalShapedQueryCompilingExpressionVisitorDependencies relationalDependencies) :
4-
base(dependencies, relationalDependencies)
5-
{
6-
}
7-
8-
public override ShapedQueryCompilingExpressionVisitor Create(QueryCompilationContext context)
6+
public override ShapedQueryCompilingExpressionVisitor Create(QueryCompilationContext context)
97
=> new RelationalVisitor(
108
Dependencies,
119
RelationalDependencies,

src/Verify.EntityFramework/MissingOrder/RelationalVisitor.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
class RelationalVisitor :
2-
RelationalShapedQueryCompilingExpressionVisitor
1+
class RelationalVisitor(
2+
ShapedQueryCompilingExpressionVisitorDependencies dependencies,
3+
RelationalShapedQueryCompilingExpressionVisitorDependencies relational,
4+
QueryCompilationContext context) :
5+
RelationalShapedQueryCompilingExpressionVisitor(dependencies, relational, context)
36
{
4-
public RelationalVisitor(ShapedQueryCompilingExpressionVisitorDependencies dependencies, RelationalShapedQueryCompilingExpressionVisitorDependencies relationalDependencies, QueryCompilationContext context) :
5-
base(dependencies, relationalDependencies, context)
6-
{
7-
}
8-
9-
[return: NotNullIfNotNull("node")]
7+
[return: NotNullIfNotNull(nameof(node))]
108
public override Expression? Visit(Expression? node)
119
{
1210
new MissingOrderByVisitor().Visit(node);

src/Verify.EntityFramework/SqlRecording.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)