Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to #18211 - Throw better exception for Last without order by #19773

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ protected override ShapedQueryExpression TranslateLastOrDefault(
var selectExpression = (SelectExpression)source.QueryExpression;
if (selectExpression.Orderings.Count == 0)
{
return null;
throw new InvalidOperationException(
CoreStrings.LastUsedWithoutOrderBy(returnDefault ?
nameof(Queryable.LastOrDefault) : nameof(Queryable.Last)));
}

if (predicate != null)
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore/Properties/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1266,4 +1266,7 @@
<data name="SkipNavigationInUseBySkipNavigation" xml:space="preserve">
<value>The skip navigation '{skipNavigation}' cannot be removed because it is set as the inverse of the skip navigation '{inverseSkipNavigation}' on '{referencingEntityType}'. All referencing skip navigations must be removed before this skip navigation can be removed.</value>
</data>
<data name="LastUsedWithoutOrderBy" xml:space="preserve">
<value>Queries performing '{method}' operation must have a deterministic sort order. Rewrite the query to apply an OrderBy clause on the sequence before calling '{method}'.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,17 @@ FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (c[""CustomerID""] = ""ALFKI""))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task LastOrDefault_when_no_order_by(bool async)
{
await base.LastOrDefault_when_no_order_by(async);

AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (c[""CustomerID""] = ""ALFKI""))");
}

public override async Task Last_Predicate(bool async)
{
await base.Last_Predicate(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ public override Task Last_when_no_order_by(bool async)
{
return base.Last_when_no_order_by(async);
}

[ConditionalTheory(Skip = "Issue#17386")]
public override Task LastOrDefault_when_no_order_by(bool async)
{
return base.LastOrDefault_when_no_order_by(async);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -976,13 +976,28 @@ public virtual Task Last(bool async)

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Last_when_no_order_by(bool async)
public virtual async Task Last_when_no_order_by(bool async)
Muppets marked this conversation as resolved.
Show resolved Hide resolved
{
return AssertTranslationFailed(
() => AssertLast(
async,
ss => ss.Set<Customer>().Where(c => c.CustomerID == "ALFKI"),
entryCount: 1));
Assert.Equal(Diagnostics.CoreStrings.LastUsedWithoutOrderBy(nameof(Enumerable.Last)),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => AssertLast(
async,
ss => ss.Set<Customer>().Where(c => c.CustomerID == "ALFKI"),
entryCount: 1)
)).Message);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task LastOrDefault_when_no_order_by(bool async)
{
Assert.Equal(Diagnostics.CoreStrings.LastUsedWithoutOrderBy(nameof(Enumerable.LastOrDefault)),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => AssertLastOrDefault(
async,
ss => ss.Set<Customer>().Where(c => c.CustomerID == "ALFKI"),
entryCount: 1)
)).Message);
}

[ConditionalTheory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ var customer
public virtual void Include_collection_with_last_no_orderby(bool useString)
{
using var context = CreateContext();
Assert.Contains(
CoreStrings.TranslationFailed("").Substring(21),
Assert.Equal(
CoreStrings.LastUsedWithoutOrderBy(nameof(Enumerable.Last)),
Assert.Throws<InvalidOperationException>(
() => useString
? context.Set<Customer>().Include("Orders").Last()
Expand Down