diff --git a/src/EFCore.Analyzers/InterpolatedStringUsageInRawQueriesCodeFixProvider.cs b/src/EFCore.Analyzers/InterpolatedStringUsageInRawQueriesCodeFixProvider.cs index b701bdcfe91..20132c8d16d 100644 --- a/src/EFCore.Analyzers/InterpolatedStringUsageInRawQueriesCodeFixProvider.cs +++ b/src/EFCore.Analyzers/InterpolatedStringUsageInRawQueriesCodeFixProvider.cs @@ -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) { diff --git a/src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs b/src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs index 05c87f1f03d..fcbeb235cad 100644 --- a/src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs +++ b/src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs @@ -263,6 +263,7 @@ public static int ExecuteSqlRaw( /// The for the context. /// The interpolated string representing a SQL query with parameters. /// The number of rows affected. + [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) @@ -485,6 +486,7 @@ public static IQueryable SqlQuery( /// A task that represents the asynchronous operation. The task result is the number of rows affected. /// /// If the is canceled. + [Obsolete("Use ExecuteSqlAsync() instead. This method is obsolete and will be removed in a future release.")] public static Task ExecuteSqlInterpolatedAsync( this DatabaseFacade databaseFacade, FormattableString sql, diff --git a/src/EFCore.Relational/Extensions/RelationalQueryableExtensions.cs b/src/EFCore.Relational/Extensions/RelationalQueryableExtensions.cs index 03e14652357..4857d14b28c 100644 --- a/src/EFCore.Relational/Extensions/RelationalQueryableExtensions.cs +++ b/src/EFCore.Relational/Extensions/RelationalQueryableExtensions.cs @@ -121,6 +121,7 @@ public static IQueryable FromSqlRaw( /// /// The interpolated string representing a SQL query with parameters. /// An representing the interpolated string SQL query. + [Obsolete("Use FromSql() instead. This method is obsolete and will be removed in a future release.")] public static IQueryable FromSqlInterpolated( this DbSet source, [NotParameterized] FormattableString sql) diff --git a/src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs b/src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs index e04943567e5..33fd6cad716 100644 --- a/src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs +++ b/src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs @@ -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<>)) diff --git a/test/EFCore.CrossStore.FunctionalTests/QueryTest.cs b/test/EFCore.CrossStore.FunctionalTests/QueryTest.cs index c2fd8837b17..f1ccaede71d 100644 --- a/test/EFCore.CrossStore.FunctionalTests/QueryTest.cs +++ b/test/EFCore.CrossStore.FunctionalTests/QueryTest.cs @@ -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) { @@ -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) diff --git a/test/EFCore.Relational.Specification.Tests/Query/FromSqlQueryTestBase.cs b/test/EFCore.Relational.Specification.Tests/Query/FromSqlQueryTestBase.cs index 5b81f8ee2a3..c5d514d9579 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/FromSqlQueryTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/FromSqlQueryTestBase.cs @@ -504,7 +504,7 @@ public virtual Task FromSqlInterpolated_queryable_with_parameters_interpolated(b return AssertQuery( async, - ss => ((DbSet)ss.Set()).FromSqlInterpolated( + ss => ((DbSet)ss.Set()).FromSql( NormalizeDelimitersInInterpolatedString( $"SELECT * FROM [Customers] WHERE [City] = {city} AND [ContactTitle] = {contactTitle}")), ss => ss.Set().Where(x => x.City == city && x.ContactTitle == contactTitle)); @@ -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)ss.Set()).FromSqlInterpolated( + ss => ((DbSet)ss.Set()).FromSql( NormalizeDelimitersInInterpolatedString( $"SELECT * FROM [Customers] WHERE [City] = {"London"} AND [ContactTitle] = {"Sales Representative"}")), ss => ss.Set().Where(x => x.City == "London" && x.ContactTitle == "Sales Representative")); @@ -554,7 +554,7 @@ await AssertQuery( async, ss => from c in ((DbSet)ss.Set()).FromSqlRaw( NormalizeDelimitersInRawString("SELECT * FROM [Customers] WHERE [City] = {0}"), city) - from o in ((DbSet)ss.Set()).FromSqlInterpolated( + from o in ((DbSet)ss.Set()).FromSql( NormalizeDelimitersInInterpolatedString( $"SELECT * FROM [Orders] WHERE [OrderDate] BETWEEN {startDate} AND {endDate}")) where c.CustomerID == o.CustomerID @@ -572,7 +572,7 @@ await AssertQuery( async, ss => from c in ((DbSet)ss.Set()).FromSqlRaw( NormalizeDelimitersInRawString("SELECT * FROM [Customers] WHERE [City] = {0}"), city) - from o in ((DbSet)ss.Set()).FromSqlInterpolated( + from o in ((DbSet)ss.Set()).FromSql( NormalizeDelimitersInInterpolatedString( $"SELECT * FROM [Orders] WHERE [OrderDate] BETWEEN {startDate} AND {endDate}")) where c.CustomerID == o.CustomerID @@ -974,8 +974,7 @@ public virtual async Task FromSqlInterpolated_with_inlined_db_parameter(bool asy await AssertQuery( async, ss => ((DbSet)ss.Set()) - .FromSqlInterpolated( - NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Customers] WHERE [CustomerID] = {parameter}")), + .FromSql(NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Customers] WHERE [CustomerID] = {parameter}")), ss => ss.Set().Where(x => x.CustomerID == "ALFKI")); } @@ -1000,8 +999,7 @@ public virtual async Task FromSqlInterpolated_with_inlined_db_parameter_without_ await AssertQuery( async, ss => ((DbSet)ss.Set()) - .FromSqlInterpolated( - NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Customers] WHERE [CustomerID] = {parameter}")), + .FromSql(NormalizeDelimitersInInterpolatedString($"SELECT * FROM [Customers] WHERE [CustomerID] = {parameter}")), ss => ss.Set().Where(x => x.CustomerID == "ALFKI")); } @@ -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 @@ -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); diff --git a/test/EFCore.Relational.Specification.Tests/Query/Inheritance/TPCInheritanceQueryTestBase.cs b/test/EFCore.Relational.Specification.Tests/Query/Inheritance/TPCInheritanceQueryTestBase.cs index 6ff3e4d7245..5f8c5df65d2 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/Inheritance/TPCInheritanceQueryTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/Inheritance/TPCInheritanceQueryTestBase.cs @@ -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(() => context.Set().FromSqlInterpolated($"Select * from Birds")) .Message; Assert.Equal(RelationalStrings.MethodOnNonTphRootNotSupported("FromSqlInterpolated", typeof(Bird).Name), message); +#pragma warning restore CS0618 message = Assert.Throws(() => context.Set().FromSql($"Select * from Birds")) .Message; diff --git a/test/EFCore.Relational.Specification.Tests/Query/Inheritance/TPTInheritanceQueryTestBase.cs b/test/EFCore.Relational.Specification.Tests/Query/Inheritance/TPTInheritanceQueryTestBase.cs index 7fc8056362c..152c90098db 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/Inheritance/TPTInheritanceQueryTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/Inheritance/TPTInheritanceQueryTestBase.cs @@ -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(() => context.Set().FromSqlInterpolated($"Select * from Birds")) .Message; Assert.Equal(RelationalStrings.MethodOnNonTphRootNotSupported("FromSqlInterpolated", typeof(Bird).Name), message); +#pragma warning restore CS0618 message = Assert.Throws(() => context.Set().FromSql($"Select * from Birds")) .Message; diff --git a/test/EFCore.Relational.Specification.Tests/Query/SqlExecutorTestBase.cs b/test/EFCore.Relational.Specification.Tests/Query/SqlExecutorTestBase.cs index db8ba7b7d02..6de82d3e512 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/SqlExecutorTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/SqlExecutorTestBase.cs @@ -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); @@ -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); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/AdHocMiscellaneousQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/AdHocMiscellaneousQuerySqlServerTest.cs index 7811c2ab7e5..856f93509f2 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/AdHocMiscellaneousQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/AdHocMiscellaneousQuerySqlServerTest.cs @@ -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 };