22using System . Data . Common ;
33using Microsoft . Data . SqlClient ;
44using Microsoft . EntityFrameworkCore . Infrastructure ;
5+ using Microsoft . EntityFrameworkCore . Storage ;
56using Thinktecture . EntityFrameworkCore . BulkOperations ;
67using Thinktecture . TestDatabaseContext ;
78
@@ -530,7 +531,7 @@ public async Task Should_create_temp_table_with_decimal()
530531 [ Fact ]
531532 public async Task Should_create_temp_table_with_decimal_with_explicit_precision ( )
532533 {
533- ConfigureModel = builder => builder . ConfigureTempTable < decimal > ( typeBuilder => typeBuilder . Property ( t => t . Column1 ) . HasPrecision ( 20 , 5 ) ) ;
534+ ConfigureModel = builder => builder . ConfigureTempTable < decimal > ( typeBuilder => typeBuilder . Property ( t => t . Column1 ) . HasPrecision ( 20 , 5 ) ) ;
534535
535536 await using var tempTable = await SUT . CreateTempTableAsync ( ActDbContext . GetTempTableEntityType < TempTable < decimal > > ( ) , _optionsWithNonUniqueName ) ;
536537
@@ -700,6 +701,26 @@ public async Task Should_create_temp_table_for_entity_with_many_owned_types()
700701 ValidateColumn ( columns [ 3 ] , nameof ( OwnedEntity . StringColumn ) , "nvarchar" , true ) ;
701702 }
702703
704+ [ Fact ]
705+ public async Task Should_honor_collation ( )
706+ {
707+ var entityType = ActDbContext . GetTempTableEntityType < TestEntityWithCollation > ( ) ;
708+ await using var tempTable = await SUT . CreateTempTableAsync ( entityType , _optionsWithNonUniqueName ) ;
709+
710+ var columns = AssertDbContext . GetTempTableColumns < TestEntityWithCollation > ( ) . ToList ( ) ;
711+ columns . Should ( ) . HaveCount ( 3 ) ;
712+
713+ var connection = AssertDbContext . Database . GetDbConnection ( ) ;
714+ await using var command = connection . CreateCommand ( ) ;
715+ command . Transaction = AssertDbContext . Database . CurrentTransaction ? . GetDbTransaction ( ) ;
716+ command . CommandText = "SELECT CONVERT (varchar(256), SERVERPROPERTY('collation'))" ;
717+ var databaseCollation = ( string ? ) await command . ExecuteScalarAsync ( ) ?? throw new Exception ( "Couldn't fetch database collection." ) ;
718+
719+ ValidateColumn ( columns [ 0 ] , nameof ( TestEntityWithCollation . Id ) , "uniqueidentifier" , false ) ;
720+ ValidateColumn ( columns [ 1 ] , nameof ( TestEntityWithCollation . ColumnWithCollation ) , "nvarchar" , false , collation : "Japanese_CI_AS" ) ;
721+ ValidateColumn ( columns [ 2 ] , nameof ( TestEntityWithCollation . ColumnWithoutCollation ) , "nvarchar" , false , collation : databaseCollation ) ;
722+ }
723+
703724 private static DbConnection CreateConnection ( )
704725 {
705726 return new SqlConnection ( TestContext . Instance . ConnectionString ) ;
@@ -718,7 +739,8 @@ private static void ValidateColumn(
718739 byte ? numericPrecision = null ,
719740 int ? numericScale = null ,
720741 int ? charMaxLength = null ,
721- string ? defaultValue = null )
742+ string ? defaultValue = null ,
743+ string ? collation = null )
722744 {
723745 ArgumentNullException . ThrowIfNull ( column ) ;
724746
@@ -727,6 +749,9 @@ private static void ValidateColumn(
727749 column . IS_NULLABLE . Should ( ) . Be ( isNullable ? "YES" : "NO" ) ;
728750 column . COLUMN_DEFAULT . Should ( ) . Be ( defaultValue ) ;
729751
752+ if ( collation is not null )
753+ column . COLLATION_NAME . Should ( ) . Be ( collation ) ;
754+
730755 if ( numericPrecision . HasValue )
731756 column . NUMERIC_PRECISION . Should ( ) . Be ( numericPrecision . Value ) ;
732757
0 commit comments