@@ -215,6 +215,7 @@ private string GetColumnsDefinitions(SqlServerTempTableCreatorCacheKey options)
215215 try
216216 {
217217 StoreObjectIdentifier ? storeObject = null ;
218+ IEntityType ? designTimeEntityType = null ;
218219
219220 var isFirst = true ;
220221
@@ -232,8 +233,22 @@ private string GetColumnsDefinitions(SqlServerTempTableCreatorCacheKey options)
232233 . Append ( _sqlGenerationHelper . DelimitIdentifier ( columnName ) ) . Append ( ' ' )
233234 . Append ( columnType ) ;
234235
235- if ( options . UseDefaultDatabaseCollation && _stringColumnTypes . Any ( t => columnType . StartsWith ( t , StringComparison . OrdinalIgnoreCase ) ) )
236- sb . Append ( " COLLATE database_default" ) ;
236+ if ( _stringColumnTypes . Any ( t => columnType . StartsWith ( t , StringComparison . OrdinalIgnoreCase ) ) )
237+ {
238+ // Collation information is not available from the runtime model, so we need to fetch it from the design time model
239+ if ( designTimeEntityType == null )
240+ {
241+ var designTimeModel = _ctx . GetService < IDesignTimeModel > ( ) . Model ;
242+ designTimeEntityType = designTimeModel . FindEntityType ( property . Property . DeclaringEntityType . Name ) ??
243+ throw new InvalidOperationException ( $ "Entity type { property . Property . DeclaringEntityType . Name } is missing from design time model.") ;
244+ }
245+ var designTimeEntityProperty = designTimeEntityType . GetProperty ( property . Property . Name ) ;
246+ var collation = designTimeEntityProperty . GetCollation ( storeObject . Value ) ;
247+ if ( string . IsNullOrWhiteSpace ( collation ) && options . UseDefaultDatabaseCollation )
248+ collation = "database_default" ;
249+ if ( ! string . IsNullOrWhiteSpace ( collation ) )
250+ sb . Append ( " COLLATE " ) . Append ( collation ) ;
251+ }
237252
238253 sb . Append ( property . Property . IsNullable ? " NULL" : " NOT NULL" ) ;
239254
0 commit comments