Skip to content
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 @@ -46,7 +46,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var foundInterpolation = false;

// Not all reported by analyzer cases are fixable. If there is a mix of interpolated arguments and normal ones, e.g. `FromSqlRaw($"SELECT * FROM [Users] WHERE [Id] = {id}", id)`,
// then replacing `FromSqlRaw` to `FromSqlInterpolated` creates compiler error since there is no overload for this.
// then replacing `FromSqlRaw` to `FromSql` creates compiler error since there is no overload for this.
// We find such cases by walking through syntaxes of each argument and searching for first interpolated string. If there are arguments after it, we consider such case unfixable.
foreach (var argument in invocationSyntax.ArgumentList.Arguments)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ public static int ExecuteSqlRaw(
/// <param name="databaseFacade">The <see cref="DatabaseFacade" /> for the context.</param>
/// <param name="sql">The interpolated string representing a SQL query with parameters.</param>
/// <returns>The number of rows affected.</returns>
[Obsolete("Use ExecuteSql() instead. This method is obsolete and will be removed in a future release.")]
public static int ExecuteSqlInterpolated(
this DatabaseFacade databaseFacade,
FormattableString sql)
Expand Down Expand Up @@ -485,6 +486,7 @@ public static IQueryable<TResult> SqlQuery<TResult>(
/// A task that represents the asynchronous operation. The task result is the number of rows affected.
/// </returns>
/// <exception cref="OperationCanceledException">If the <see cref="CancellationToken" /> is canceled.</exception>
[Obsolete("Use ExecuteSqlAsync() instead. This method is obsolete and will be removed in a future release.")]
public static Task<int> ExecuteSqlInterpolatedAsync(
this DatabaseFacade databaseFacade,
FormattableString sql,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public static IQueryable<TEntity> FromSqlRaw<TEntity>(
/// </param>
/// <param name="sql">The interpolated string representing a SQL query with parameters.</param>
/// <returns>An <see cref="IQueryable{T}" /> representing the interpolated string SQL query.</returns>
[Obsolete("Use FromSql() instead. This method is obsolete and will be removed in a future release.")]
public static IQueryable<TEntity> FromSqlInterpolated<TEntity>(
this DbSet<TEntity> source,
[NotParameterized] FormattableString sql)
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ protected override Expression VisitMember(MemberExpression member)
// roundtrip.
// Note that we only do this when the MemberExpression is typed as IQueryable/IOrderedQueryable; this notably excludes
// DbSet captured variables integrated directly into the query, as that also evaluates e.g. context.Order in
// context.Order.FromSqlInterpolated(), which fails.
// context.Order.FromSql(), which fails.
if (member.Type.IsConstructedGenericType
&& member.Type.GetGenericTypeDefinition() is var genericTypeDefinition
&& (genericTypeDefinition == typeof(IQueryable<>) || genericTypeDefinition == typeof(IOrderedQueryable<>))
Expand Down
2 changes: 2 additions & 0 deletions test/EFCore.CrossStore.FunctionalTests/QueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public async Task Cosmos_FromSqlRaw_throws_for_InMemory(bool async)
Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(FromSqlQueryRootExpression)), message);
}

#pragma warning disable CS0618 // FromSqlInterpolated is obsolete
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
public async Task FromSqlInterpolated_throws_for_InMemory(bool async)
{
Expand All @@ -80,6 +81,7 @@ public async Task FromSqlInterpolated_throws_for_InMemory(bool async)

Assert.Equal(CoreStrings.QueryUnhandledQueryRootExpression(nameof(FromSqlQueryRootExpression)), message);
}
#pragma warning restore CS0618

[ConditionalTheory, MemberData(nameof(IsAsyncData))]
public async Task FromSql_throws_for_InMemory(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ public virtual Task FromSqlInterpolated_queryable_with_parameters_interpolated(b

return AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSqlInterpolated(
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSql(
NormalizeDelimitersInInterpolatedString(
$"SELECT * FROM [Customers] WHERE [City] = {city} AND [ContactTitle] = {contactTitle}")),
ss => ss.Set<Customer>().Where(x => x.City == city && x.ContactTitle == contactTitle));
Expand All @@ -528,7 +528,7 @@ public virtual Task FromSql_queryable_with_parameters_interpolated(bool async)
public virtual Task FromSqlInterpolated_queryable_with_parameters_inline_interpolated(bool async)
=> AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSqlInterpolated(
ss => ((DbSet<Customer>)ss.Set<Customer>()).FromSql(
NormalizeDelimitersInInterpolatedString(
$"SELECT * FROM [Customers] WHERE [City] = {"London"} AND [ContactTitle] = {"Sales Representative"}")),
ss => ss.Set<Customer>().Where(x => x.City == "London" && x.ContactTitle == "Sales Representative"));
Expand All @@ -554,7 +554,7 @@ await AssertQuery(
async,
ss => from c in ((DbSet<Customer>)ss.Set<Customer>()).FromSqlRaw(
NormalizeDelimitersInRawString("SELECT * FROM [Customers] WHERE [City] = {0}"), city)
from o in ((DbSet<Order>)ss.Set<Order>()).FromSqlInterpolated(
from o in ((DbSet<Order>)ss.Set<Order>()).FromSql(
NormalizeDelimitersInInterpolatedString(
$"SELECT * FROM [Orders] WHERE [OrderDate] BETWEEN {startDate} AND {endDate}"))
where c.CustomerID == o.CustomerID
Expand All @@ -572,7 +572,7 @@ await AssertQuery(
async,
ss => from c in ((DbSet<Customer>)ss.Set<Customer>()).FromSqlRaw(
NormalizeDelimitersInRawString("SELECT * FROM [Customers] WHERE [City] = {0}"), city)
from o in ((DbSet<Order>)ss.Set<Order>()).FromSqlInterpolated(
from o in ((DbSet<Order>)ss.Set<Order>()).FromSql(
NormalizeDelimitersInInterpolatedString(
$"SELECT * FROM [Orders] WHERE [OrderDate] BETWEEN {startDate} AND {endDate}"))
where c.CustomerID == o.CustomerID
Expand Down Expand Up @@ -974,8 +974,7 @@ public virtual async Task FromSqlInterpolated_with_inlined_db_parameter(bool asy
await AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>())
.FromSqlInterpolated(
NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Customers] WHERE [CustomerID] = {parameter}")),
.FromSql(NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Customers] WHERE [CustomerID] = {parameter}")),
ss => ss.Set<Customer>().Where(x => x.CustomerID == "ALFKI"));
}

Expand All @@ -1000,8 +999,7 @@ public virtual async Task FromSqlInterpolated_with_inlined_db_parameter_without_
await AssertQuery(
async,
ss => ((DbSet<Customer>)ss.Set<Customer>())
.FromSqlInterpolated(
NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Customers] WHERE [CustomerID] = {parameter}")),
.FromSql(NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Customers] WHERE [CustomerID] = {parameter}")),
ss => ss.Set<Customer>().Where(x => x.CustomerID == "ALFKI"));
}

Expand All @@ -1026,7 +1024,7 @@ public virtual async Task FromSqlInterpolated_parameterization_issue_12213(bool
var max = 10400;

var query1 = context.Orders
.FromSqlInterpolated(NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Orders] WHERE [OrderID] >= {min}"))
.FromSql(NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Orders] WHERE [OrderID] >= {min}"))
.Select(i => i.OrderID);

var actual1 = async
Expand All @@ -1044,8 +1042,7 @@ public virtual async Task FromSqlInterpolated_parameterization_issue_12213(bool
var query3 = context.Orders
.Where(o => o.OrderID <= max
&& context.Orders
.FromSqlInterpolated(
NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Orders] WHERE [OrderID] >= {min}"))
.FromSql(NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Orders] WHERE [OrderID] >= {min}"))
.Select(i => i.OrderID)
.Contains(o.OrderID))
.Select(o => o.OrderID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ public virtual void Using_from_sql_throws()

Assert.Equal(RelationalStrings.MethodOnNonTphRootNotSupported("FromSqlRaw", typeof(Bird).Name), message);

#pragma warning disable CS0618 // FromSqlInterpolated is obsolete
message = Assert.Throws<InvalidOperationException>(() => context.Set<Bird>().FromSqlInterpolated($"Select * from Birds"))
.Message;

Assert.Equal(RelationalStrings.MethodOnNonTphRootNotSupported("FromSqlInterpolated", typeof(Bird).Name), message);
#pragma warning restore CS0618

message = Assert.Throws<InvalidOperationException>(() => context.Set<Bird>().FromSql($"Select * from Birds"))
.Message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ public virtual void Using_from_sql_throws()

Assert.Equal(RelationalStrings.MethodOnNonTphRootNotSupported("FromSqlRaw", typeof(Bird).Name), message);

#pragma warning disable CS0618 // FromSqlInterpolated is obsolete
message = Assert.Throws<InvalidOperationException>(() => context.Set<Bird>().FromSqlInterpolated($"Select * from Birds"))
.Message;

Assert.Equal(RelationalStrings.MethodOnNonTphRootNotSupported("FromSqlInterpolated", typeof(Bird).Name), message);
#pragma warning restore CS0618

message = Assert.Throws<InvalidOperationException>(() => context.Set<Bird>().FromSql($"Select * from Birds"))
.Message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ public virtual async Task Query_with_parameters_interpolated(bool async)
using var context = CreateContext();

var actual = async
? await context.Database.ExecuteSqlInterpolatedAsync(
? await context.Database.ExecuteSqlAsync(
$@"SELECT COUNT(*) FROM ""Customers"" WHERE ""City"" = {city} AND ""ContactTitle"" = {contactTitle}")
: context.Database.ExecuteSqlInterpolated(
: context.Database.ExecuteSql(
$@"SELECT COUNT(*) FROM ""Customers"" WHERE ""City"" = {city} AND ""ContactTitle"" = {contactTitle}");

Assert.Equal(-1, actual);
Expand All @@ -209,9 +209,9 @@ public virtual async Task Query_with_DbParameters_interpolated(bool async)
using var context = CreateContext();

var actual = async
? await context.Database.ExecuteSqlInterpolatedAsync(
? await context.Database.ExecuteSqlAsync(
$@"SELECT COUNT(*) FROM ""Customers"" WHERE ""City"" = {city} AND ""ContactTitle"" = {contactTitle}")
: context.Database.ExecuteSqlInterpolated(
: context.Database.ExecuteSql(
$@"SELECT COUNT(*) FROM ""Customers"" WHERE ""City"" = {city} AND ""ContactTitle"" = {contactTitle}");

Assert.Equal(-1, actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1110,9 +1110,9 @@ public virtual async Task From_sql_expression_compares_correctly()

using (var context = contextFactory.CreateContext())
{
var query = from t1 in context.Tests.FromSqlInterpolated(
var query = from t1 in context.Tests.FromSql(
$"Select * from Tests Where Type = {Context19206.TestType19206.Unit}")
from t2 in context.Tests.FromSqlInterpolated(
from t2 in context.Tests.FromSql(
$"Select * from Tests Where Type = {Context19206.TestType19206.Integration}")
select new { t1, t2 };

Expand Down
Loading