From 04c8a584de0ba7c181019d7127765964fda3a56a Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Mon, 30 Dec 2019 11:37:37 -0800 Subject: [PATCH] Update SQL Server type mappings for strings Fixes #19327 Create fixed-length type mappings by default, and then use the correct DbType in the parameter if the length of the value matches the parameter length. This has a better chance of hitting an index. In other cases, change the DbType to match what we did before so that SQLClient doesn't truncate or pad. Fixes #14555 If the parameter value length is greater than the max length but less than the max bounded length, then using the max bounded length rather than going immediately to unbounded. --- .../Internal/SqlServerByteArrayTypeMapping.cs | 31 ++++++-- .../Internal/SqlServerStringTypeMapping.cs | 75 ++++++++++++++----- .../BuiltInDataTypesSqlServerTest.cs | 32 ++++---- .../DataAnnotationSqlServerTest.cs | 4 +- .../NorthwindCompiledQuerySqlServerTest.cs | 18 ++--- ...orthwindMiscellaneousQuerySqlServerTest.cs | 8 +- ...NorthwindQueryFiltersQuerySqlServerTest.cs | 6 +- .../Query/NorthwindWhereQuerySqlServerTest.cs | 6 +- .../SqlServerTypeMapperTest.cs | 28 +++---- 9 files changed, 129 insertions(+), 79 deletions(-) diff --git a/src/EFCore.SqlServer/Storage/Internal/SqlServerByteArrayTypeMapping.cs b/src/EFCore.SqlServer/Storage/Internal/SqlServerByteArrayTypeMapping.cs index e5448f6f5b2..1fe4f609685 100644 --- a/src/EFCore.SqlServer/Storage/Internal/SqlServerByteArrayTypeMapping.cs +++ b/src/EFCore.SqlServer/Storage/Internal/SqlServerByteArrayTypeMapping.cs @@ -81,11 +81,6 @@ protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters p /// protected override void ConfigureParameter(DbParameter parameter) { - // For strings and byte arrays, set the max length to the size facet if specified, or - // 8000 bytes if no size facet specified, if the data will fit so as to avoid query cache - // fragmentation by setting lots of different Size values otherwise always set to - // -1 (unbounded) to avoid SQL client size inference. - var value = parameter.Value; var length = (value as byte[])?.Length; var maxSpecificSize = CalculateSize(Size); @@ -96,9 +91,29 @@ protected override void ConfigureParameter(DbParameter parameter) sqlParameter.SqlDbType = _sqlDbType.Value; } - parameter.Size = value == null || value == DBNull.Value || length != null && length <= maxSpecificSize - ? maxSpecificSize - : -1; + if (value == null + || value == DBNull.Value) + { + parameter.Size = maxSpecificSize; + } + else + { + if (length != null + && length <= maxSpecificSize) + { + // Fixed-sized parameters get exact length to avoid padding/truncation. + parameter.Size = IsFixedLength ? length.Value : maxSpecificSize; + } + else if (length != null + && length <= MaxSize) + { + parameter.Size = IsFixedLength ? length.Value : MaxSize; + } + else + { + parameter.Size = -1; + } + } } /// diff --git a/src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs b/src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs index aa5a33b1901..fa74d422ae8 100644 --- a/src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs +++ b/src/EFCore.SqlServer/Storage/Internal/SqlServerStringTypeMapping.cs @@ -23,6 +23,7 @@ public class SqlServerStringTypeMapping : StringTypeMapping private readonly SqlDbType? _sqlDbType; private readonly int _maxSpecificSize; + private readonly int _maxSize; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -57,8 +58,12 @@ private static string GetStoreName(bool unicode, bool fixedLength) => unicode : "varchar"; private static DbType? GetDbType(bool unicode, bool fixedLength) => unicode - ? (fixedLength ? System.Data.DbType.String : (DbType?)null) - : System.Data.DbType.AnsiString; + ? (fixedLength + ? System.Data.DbType.StringFixedLength + : (DbType?)null) + : (fixedLength + ? System.Data.DbType.AnsiStringFixedLength + : System.Data.DbType.AnsiString); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -69,19 +74,20 @@ private static string GetStoreName(bool unicode, bool fixedLength) => unicode protected SqlServerStringTypeMapping(RelationalTypeMappingParameters parameters, SqlDbType? sqlDbType) : base(parameters) { - _maxSpecificSize = CalculateSize(parameters.Unicode, parameters.Size); + if (parameters.Unicode) + { + _maxSpecificSize = parameters.Size.HasValue && parameters.Size <= UnicodeMax ? parameters.Size.Value : UnicodeMax; + _maxSize = UnicodeMax; + } + else + { + _maxSpecificSize = parameters.Size.HasValue && parameters.Size <= AnsiMax ? parameters.Size.Value : AnsiMax; + _maxSize = AnsiMax; + } + _sqlDbType = sqlDbType; } - private static int CalculateSize(bool unicode, int? size) - => unicode - ? size.HasValue && size <= UnicodeMax - ? size.Value - : UnicodeMax - : size.HasValue && size <= AnsiMax - ? size.Value - : AnsiMax; - /// /// Creates a copy of this mapping. /// @@ -98,11 +104,6 @@ protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters p /// protected override void ConfigureParameter(DbParameter parameter) { - // For strings and byte arrays, set the max length to the size facet if specified, or - // 8000 bytes if no size facet specified, if the data will fit so as to avoid query cache - // fragmentation by setting lots of different Size values otherwise always set to - // -1 (unbounded) to avoid SQL client size inference. - var value = parameter.Value; var length = (value as string)?.Length; @@ -112,9 +113,43 @@ protected override void ConfigureParameter(DbParameter parameter) sqlParameter.SqlDbType = _sqlDbType.Value; } - parameter.Size = value == null || value == DBNull.Value || length != null && length <= _maxSpecificSize - ? _maxSpecificSize - : -1; + if ((value == null + || value == DBNull.Value) + || (IsFixedLength + && length == _maxSpecificSize + && Size.HasValue)) + { + // A fixed-length parameter where the value matches the length can remain a fixed-length parameter + // because SQLClient will not do any padding or truncating. + parameter.Size = _maxSpecificSize; + } + else + { + if (IsFixedLength) + { + // Force the parameter type to be not fixed length to avoid SQLClient truncation and padding. + parameter.DbType = IsUnicode ? System.Data.DbType.String : System.Data.DbType.AnsiString; + } + + // For strings and byte arrays, set the max length to the size facet if specified, or + // 8000 bytes if no size facet specified, if the data will fit so as to avoid query cache + // fragmentation by setting lots of different Size values otherwise set to the max bounded length + // if the value will fit, otherwise set to -1 (unbounded) to avoid SQL client size inference. + if (length != null + && length <= _maxSpecificSize) + { + parameter.Size = _maxSpecificSize; + } + else if (length != null + && length <= _maxSize) + { + parameter.Size = _maxSize; + } + else + { + parameter.Size = -1; + } + } } /// diff --git a/test/EFCore.SqlServer.FunctionalTests/BuiltInDataTypesSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/BuiltInDataTypesSqlServerTest.cs index cf632afa476..1aa390f316c 100644 --- a/test/EFCore.SqlServer.FunctionalTests/BuiltInDataTypesSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/BuiltInDataTypesSqlServerTest.cs @@ -1180,14 +1180,14 @@ public virtual void Can_insert_and_read_back_all_mapped_sized_data_types() @p7='F' (Size = 3) @p8='D' (Size = 3) @p9='A' (Size = 3) (DbType = AnsiString) -@p10='Wor' (Size = 3) (DbType = AnsiString) +@p10='Wor' (Size = 3) (DbType = AnsiStringFixedLength) @p11='Thr' (Size = 3) (DbType = AnsiString) -@p12='Lon' (Size = 3) (DbType = AnsiString) +@p12='Lon' (Size = 3) (DbType = AnsiStringFixedLength) @p13='Let' (Size = 3) (DbType = AnsiString) @p14='The' (Size = 3) -@p15='Squ' (Size = 3) +@p15='Squ' (Size = 3) (DbType = StringFixedLength) @p16='Col' (Size = 3) -@p17='Won' (Size = 3) +@p17='Won' (Size = 3) (DbType = StringFixedLength) @p18='Int' (Size = 3) @p19='Tha' (Size = 3) (DbType = AnsiString)", parameters, @@ -1270,14 +1270,14 @@ public virtual void Can_insert_and_read_back_nulls_for_all_mapped_sized_data_typ @p7=NULL (Size = 3) @p8=NULL (Size = 3) @p9=NULL (Size = 3) (DbType = AnsiString) -@p10=NULL (Size = 3) (DbType = AnsiString) +@p10=NULL (Size = 3) (DbType = AnsiStringFixedLength) @p11=NULL (Size = 3) (DbType = AnsiString) -@p12=NULL (Size = 3) (DbType = AnsiString) +@p12=NULL (Size = 3) (DbType = AnsiStringFixedLength) @p13=NULL (Size = 3) (DbType = AnsiString) @p14=NULL (Size = 3) -@p15=NULL (Size = 3) +@p15=NULL (Size = 3) (DbType = StringFixedLength) @p16=NULL (Size = 3) -@p17=NULL (Size = 3) +@p17=NULL (Size = 3) (DbType = StringFixedLength) @p18=NULL (Size = 3) @p19=NULL (Size = 3) (DbType = AnsiString)", parameters, @@ -1940,14 +1940,14 @@ public virtual void Can_insert_and_read_back_all_mapped_sized_data_types_with_id @p7='D' (Size = 3) @p8='A' (Size = 3) (DbType = AnsiString) @p9='77' -@p10='Wor' (Size = 3) (DbType = AnsiString) +@p10='Wor' (Size = 3) (DbType = AnsiStringFixedLength) @p11='Thr' (Size = 3) (DbType = AnsiString) -@p12='Lon' (Size = 3) (DbType = AnsiString) +@p12='Lon' (Size = 3) (DbType = AnsiStringFixedLength) @p13='Let' (Size = 3) (DbType = AnsiString) @p14='The' (Size = 3) -@p15='Squ' (Size = 3) +@p15='Squ' (Size = 3) (DbType = StringFixedLength) @p16='Col' (Size = 3) -@p17='Won' (Size = 3) +@p17='Won' (Size = 3) (DbType = StringFixedLength) @p18='Int' (Size = 3) @p19='Tha' (Size = 3) (DbType = AnsiString)", parameters, @@ -2030,14 +2030,14 @@ public virtual void Can_insert_and_read_back_nulls_for_all_mapped_sized_data_typ @p7=NULL (Size = 3) @p8=NULL (Size = 3) (DbType = AnsiString) @p9='78' -@p10=NULL (Size = 3) (DbType = AnsiString) +@p10=NULL (Size = 3) (DbType = AnsiStringFixedLength) @p11=NULL (Size = 3) (DbType = AnsiString) -@p12=NULL (Size = 3) (DbType = AnsiString) +@p12=NULL (Size = 3) (DbType = AnsiStringFixedLength) @p13=NULL (Size = 3) (DbType = AnsiString) @p14=NULL (Size = 3) -@p15=NULL (Size = 3) +@p15=NULL (Size = 3) (DbType = StringFixedLength) @p16=NULL (Size = 3) -@p17=NULL (Size = 3) +@p17=NULL (Size = 3) (DbType = StringFixedLength) @p18=NULL (Size = 3) @p19=NULL (Size = 3) (DbType = AnsiString)", parameters, diff --git a/test/EFCore.SqlServer.FunctionalTests/DataAnnotationSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/DataAnnotationSqlServerTest.cs index 996ceb181f5..ab687d82d6f 100644 --- a/test/EFCore.SqlServer.FunctionalTests/DataAnnotationSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/DataAnnotationSqlServerTest.cs @@ -238,7 +238,7 @@ SELECT [UniqueNo] FROM [Sample] WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();", // - @"@p0='VeryVeryVeryVeryVeryVeryLongString' (Size = -1) + @"@p0='VeryVeryVeryVeryVeryVeryLongString' (Size = 4000) @p1='ValidString' (Nullable = false) (Size = 4000) @p2='00000000-0000-0000-0000-000000000002' @p3='Third Additional Name' (Size = 4000) @@ -324,7 +324,7 @@ INSERT INTO [Two] ([Data]) FROM [Two] WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();", // - @"@p0='ValidButLongString' (Size = -1) + @"@p0='ValidButLongString' (Size = 4000) SET NOCOUNT ON; INSERT INTO [Two] ([Data]) diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindCompiledQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindCompiledQuerySqlServerTest.cs index 7bd7c9545d1..51895940a2e 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindCompiledQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindCompiledQuerySqlServerTest.cs @@ -66,13 +66,13 @@ public override void Query_with_single_parameter() base.Query_with_single_parameter(); AssertSql( - @"@__customerID='ALFKI' (Size = 5) + @"@__customerID='ALFKI' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] WHERE [c].[CustomerID] = @__customerID", // - @"@__customerID='ANATR' (Size = 5) + @"@__customerID='ANATR' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] @@ -84,13 +84,13 @@ public override void First_query_with_single_parameter() base.First_query_with_single_parameter(); AssertSql( - @"@__customerID='ALFKI' (Size = 5) + @"@__customerID='ALFKI' (Size = 5) (DbType = StringFixedLength) SELECT TOP(1) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] WHERE [c].[CustomerID] = @__customerID", // - @"@__customerID='ANATR' (Size = 5) + @"@__customerID='ANATR' (Size = 5) (DbType = StringFixedLength) SELECT TOP(1) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] @@ -102,13 +102,13 @@ public override void Query_with_two_parameters() base.Query_with_two_parameters(); AssertSql( - @"@__customerID='ALFKI' (Size = 5) + @"@__customerID='ALFKI' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] WHERE [c].[CustomerID] = @__customerID", // - @"@__customerID='ANATR' (Size = 5) + @"@__customerID='ANATR' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] @@ -120,13 +120,13 @@ public override void Query_with_three_parameters() base.Query_with_three_parameters(); AssertSql( - @"@__customerID='ALFKI' (Size = 5) + @"@__customerID='ALFKI' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] WHERE [c].[CustomerID] = @__customerID", // - @"@__customerID='ANATR' (Size = 5) + @"@__customerID='ANATR' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] @@ -166,7 +166,7 @@ public override void Compiled_query_when_does_not_end_in_query_operator() base.Compiled_query_when_does_not_end_in_query_operator(); AssertSql( - @"@__customerID='ALFKI' (Size = 5) + @"@__customerID='ALFKI' (Size = 5) (DbType = StringFixedLength) SELECT COUNT(*) FROM [Customers] AS [c] diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs index 99cc055b866..3e7d148376f 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs @@ -137,7 +137,7 @@ public override async Task Local_dictionary(bool async) await base.Local_dictionary(async); AssertSql( - @"@__p_0='ALFKI' (Size = 5) + @"@__p_0='ALFKI' (Size = 5) (DbType = StringFixedLength) SELECT TOP(2) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] @@ -158,7 +158,7 @@ public override async Task Entity_equality_local(bool async) await base.Entity_equality_local(async); AssertSql( - @"@__entity_equality_local_0_CustomerID='ANATR' (Size = 5) + @"@__entity_equality_local_0_CustomerID='ANATR' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID] FROM [Customers] AS [c] @@ -183,7 +183,7 @@ public override async Task Entity_equality_local_double_check(bool async) await base.Entity_equality_local_double_check(async); AssertSql( - @"@__entity_equality_local_0_CustomerID='ANATR' (Size = 5) + @"@__entity_equality_local_0_CustomerID='ANATR' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID] FROM [Customers] AS [c] @@ -195,7 +195,7 @@ public override async Task Join_with_entity_equality_local_on_both_sources(bool await base.Join_with_entity_equality_local_on_both_sources(async); AssertSql( - @"@__entity_equality_local_0_CustomerID='ANATR' (Size = 5) + @"@__entity_equality_local_0_CustomerID='ANATR' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID] FROM [Customers] AS [c] diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindQueryFiltersQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindQueryFiltersQuerySqlServerTest.cs index 8e7f315f5da..68318f5d6f4 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindQueryFiltersQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindQueryFiltersQuerySqlServerTest.cs @@ -47,7 +47,7 @@ public override void Find() AssertSql( @"@__ef_filter__TenantPrefix_0='B' (Size = 4000) -@__p_0='ALFKI' (Size = 5) +@__p_0='ALFKI' (Size = 5) (DbType = StringFixedLength) SELECT TOP(1) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] @@ -261,14 +261,14 @@ public override void Compiled_query() AssertSql( @"@__ef_filter__TenantPrefix_0='B' (Size = 4000) -@__customerID='BERGS' (Size = 5) +@__customerID='BERGS' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] WHERE ((@__ef_filter__TenantPrefix_0 = N'') OR ([c].[CompanyName] IS NOT NULL AND (LEFT([c].[CompanyName], LEN(@__ef_filter__TenantPrefix_0)) = @__ef_filter__TenantPrefix_0))) AND ([c].[CustomerID] = @__customerID)", // @"@__ef_filter__TenantPrefix_0='B' (Size = 4000) -@__customerID='BLAUS' (Size = 5) +@__customerID='BLAUS' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs index 18f0dcfbb91..6efa8b4e010 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs @@ -386,7 +386,7 @@ public override void Where_subquery_closure_via_query_cache() base.Where_subquery_closure_via_query_cache(); AssertSql( - @"@__customerID_0='ALFKI' (Size = 5) + @"@__customerID_0='ALFKI' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] @@ -395,7 +395,7 @@ SELECT 1 FROM [Orders] AS [o] WHERE ([o].[CustomerID] = @__customerID_0) AND ([o].[CustomerID] = [c].[CustomerID]))", // - @"@__customerID_0='ANATR' (Size = 5) + @"@__customerID_0='ANATR' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] @@ -1545,7 +1545,7 @@ public override async Task Where_array_index(bool async) await base.Where_array_index(async); AssertSql( - @"@__p_0='ALFKI' (Size = 5) + @"@__p_0='ALFKI' (Size = 5) (DbType = StringFixedLength) SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] diff --git a/test/EFCore.SqlServer.Tests/SqlServerTypeMapperTest.cs b/test/EFCore.SqlServer.Tests/SqlServerTypeMapperTest.cs index e59c7750e39..44bf0c09103 100644 --- a/test/EFCore.SqlServer.Tests/SqlServerTypeMapperTest.cs +++ b/test/EFCore.SqlServer.Tests/SqlServerTypeMapperTest.cs @@ -174,7 +174,7 @@ public void Does_non_key_SQL_Server_string_mapping_with_max_length(bool? unicode Assert.Equal(3, typeMapping.Size); Assert.True(typeMapping.IsUnicode); Assert.False(typeMapping.IsFixedLength); - Assert.Equal(-1, typeMapping.CreateParameter(new TestCommand(), "Name", "Value").Size); + Assert.Equal(4000, typeMapping.CreateParameter(new TestCommand(), "Name", "Value").Size); } [ConditionalTheory] @@ -184,7 +184,7 @@ public void Does_non_key_SQL_Server_fixed_string_mapping_with_max_length_large_v { var typeMapping = GetTypeMapping(typeof(string), null, 3, unicode, fixedLength: true); - Assert.Equal(DbType.String, typeMapping.DbType); + Assert.Equal(DbType.StringFixedLength, typeMapping.DbType); Assert.Equal("nchar(3)", typeMapping.StoreType); Assert.Equal(3, typeMapping.Size); Assert.True(typeMapping.IsUnicode); @@ -192,7 +192,7 @@ public void Does_non_key_SQL_Server_fixed_string_mapping_with_max_length_large_v var parameter = typeMapping.CreateParameter(new TestCommand(), "Name", "Value"); Assert.Equal(DbType.String, parameter.DbType); - Assert.Equal(-1, parameter.Size); + Assert.Equal(4000, parameter.Size); } [ConditionalTheory] @@ -202,7 +202,7 @@ public void Does_non_key_SQL_Server_fixed_string_mapping_with_max_length_small_v { var typeMapping = GetTypeMapping(typeof(string), null, 3, unicode, fixedLength: true); - Assert.Equal(DbType.String, typeMapping.DbType); + Assert.Equal(DbType.StringFixedLength, typeMapping.DbType); Assert.Equal("nchar(3)", typeMapping.StoreType); Assert.Equal(3, typeMapping.Size); Assert.True(typeMapping.IsUnicode); @@ -220,14 +220,14 @@ public void Does_non_key_SQL_Server_fixed_string_mapping_with_max_length_exact_v { var typeMapping = GetTypeMapping(typeof(string), null, 3, unicode, fixedLength: true); - Assert.Equal(DbType.String, typeMapping.DbType); + Assert.Equal(DbType.StringFixedLength, typeMapping.DbType); Assert.Equal("nchar(3)", typeMapping.StoreType); Assert.Equal(3, typeMapping.Size); Assert.True(typeMapping.IsUnicode); Assert.True(typeMapping.IsFixedLength); var parameter = typeMapping.CreateParameter(new TestCommand(), "Name", "Val"); - Assert.Equal(DbType.String, parameter.DbType); + Assert.Equal(DbType.StringFixedLength, parameter.DbType); Assert.Equal(3, parameter.Size); } @@ -426,7 +426,7 @@ public void Does_non_key_SQL_Server_string_mapping_with_max_length_ansi(bool? fi Assert.Equal(3, typeMapping.Size); Assert.False(typeMapping.IsUnicode); Assert.False(typeMapping.IsFixedLength); - Assert.Equal(-1, typeMapping.CreateParameter(new TestCommand(), "Name", "Value").Size); + Assert.Equal(8000, typeMapping.CreateParameter(new TestCommand(), "Name", "Value").Size); } [ConditionalFact] @@ -434,7 +434,7 @@ public void Does_non_key_SQL_Server_fixed_string_mapping_with_max_length_ansi_la { var typeMapping = GetTypeMapping(typeof(string), null, 3, unicode: false, fixedLength: true); - Assert.Equal(DbType.AnsiString, typeMapping.DbType); + Assert.Equal(DbType.AnsiStringFixedLength, typeMapping.DbType); Assert.Equal("char(3)", typeMapping.StoreType); Assert.Equal(3, typeMapping.Size); Assert.False(typeMapping.IsUnicode); @@ -442,7 +442,7 @@ public void Does_non_key_SQL_Server_fixed_string_mapping_with_max_length_ansi_la var parameter = typeMapping.CreateParameter(new TestCommand(), "Name", "Value"); Assert.Equal(DbType.AnsiString, parameter.DbType); - Assert.Equal(-1, parameter.Size); + Assert.Equal(8000, parameter.Size); } [ConditionalFact] @@ -450,7 +450,7 @@ public void Does_non_key_SQL_Server_fixed_string_mapping_with_max_length_ansi_sm { var typeMapping = GetTypeMapping(typeof(string), null, 3, unicode: false, fixedLength: true); - Assert.Equal(DbType.AnsiString, typeMapping.DbType); + Assert.Equal(DbType.AnsiStringFixedLength, typeMapping.DbType); Assert.Equal("char(3)", typeMapping.StoreType); Assert.Equal(3, typeMapping.Size); Assert.False(typeMapping.IsUnicode); @@ -466,14 +466,14 @@ public void Does_non_key_SQL_Server_fixed_string_mapping_with_max_length_ansi_ex { var typeMapping = GetTypeMapping(typeof(string), null, 3, unicode: false, fixedLength: true); - Assert.Equal(DbType.AnsiString, typeMapping.DbType); + Assert.Equal(DbType.AnsiStringFixedLength, typeMapping.DbType); Assert.Equal("char(3)", typeMapping.StoreType); Assert.Equal(3, typeMapping.Size); Assert.False(typeMapping.IsUnicode); Assert.True(typeMapping.IsFixedLength); var parameter = typeMapping.CreateParameter(new TestCommand(), "Name", "Val"); - Assert.Equal(DbType.AnsiString, parameter.DbType); + Assert.Equal(DbType.AnsiStringFixedLength, parameter.DbType); Assert.Equal(3, parameter.Size); } @@ -696,7 +696,7 @@ public void Does_non_key_SQL_Server_fixed_length_binary_mapping_with_small_value var parameter = typeMapping.CreateParameter(new TestCommand(), "Name", new byte[10]); Assert.Equal(DbType.Binary, parameter.DbType); - Assert.Equal(100, parameter.Size); + Assert.Equal(10, parameter.Size); } [ConditionalTheory] @@ -732,7 +732,7 @@ public void Does_non_key_SQL_Server_fixed_length_binary_mapping_with_large_value var parameter = typeMapping.CreateParameter(new TestCommand(), "Name", new byte[101]); Assert.Equal(DbType.Binary, parameter.DbType); - Assert.Equal(-1, parameter.Size); + Assert.Equal(101, parameter.Size); } [ConditionalTheory]