From f97e6341abe35d7d0af259cc3dad4e02def15817 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Tue, 28 Nov 2017 17:22:37 -0800 Subject: [PATCH] Query: Preserve Schema in SqlFunctionExpression while visiting children Issue was fixed in #10370 VisitChildren method was incorrect This adds test for the scenario Resolves #10421 --- .../Query/DbFunctionsTestBase.cs | 2 +- .../Query/UdfDbFunctionSqlServerTests.cs | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/EFCore.Specification.Tests/Query/DbFunctionsTestBase.cs b/src/EFCore.Specification.Tests/Query/DbFunctionsTestBase.cs index ecfa66afc77..12860cf6db9 100644 --- a/src/EFCore.Specification.Tests/Query/DbFunctionsTestBase.cs +++ b/src/EFCore.Specification.Tests/Query/DbFunctionsTestBase.cs @@ -45,7 +45,7 @@ public virtual void String_Like_Literal_With_Escape() using (var context = CreateContext()) { var count = context.Customers.Count(c => EF.Functions.Like(c.ContactName, "!%", "!")); - + Assert.Equal(0, count); } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/UdfDbFunctionSqlServerTests.cs b/test/EFCore.SqlServer.FunctionalTests/Query/UdfDbFunctionSqlServerTests.cs index b978dec8243..90ded012b4a 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/UdfDbFunctionSqlServerTests.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/UdfDbFunctionSqlServerTests.cs @@ -192,6 +192,12 @@ public string DollarValueInstance(int starCount, string value) throw new NotImplementedException(); } + [DbFunction(Schema = "dbo")] + public static string IdentityString(string s) + { + throw new NotImplementedException(); + } + #endregion public UDFSqlContext(DbContextOptions options) @@ -855,6 +861,25 @@ FROM [Customers] AS [c] } } + [Fact] + public void Nullable_navigation_property_access_preserves_schema_for_sql_function() + { + using (var context = CreateContext()) + { + var result = context.Orders + .OrderBy(o => o.Id) + .Select(o => UDFSqlContext.IdentityString(o.Customer.FirstName)) + .FirstOrDefault(); + + Assert.Equal("Customer", result); + AssertSql( + @"SELECT TOP(1) [dbo].IdentityString([o.Customer].[FirstName]) +FROM [Orders] AS [o] +LEFT JOIN [Customers] AS [o.Customer] ON [o].[CustomerId] = [o.Customer].[Id] +ORDER BY [o].[Id]"); + } + } + #endregion #region Instance @@ -1571,6 +1596,13 @@ returns bit return 0 end"); + context.Database.ExecuteSqlCommand(@"create function [dbo].[IdentityString] (@customerName nvarchar(max)) + returns nvarchar(max) + as + begin + return @customerName; + end"); + var order11 = new Order { Name = "Order11", ItemCount = 4, OrderDate = new DateTime(2000, 1, 20) }; var order12 = new Order { Name = "Order12", ItemCount = 8, OrderDate = new DateTime(2000, 2, 21) }; var order13 = new Order { Name = "Order13", ItemCount = 15, OrderDate = new DateTime(2000, 3, 20) };