Skip to content

Commit

Permalink
Merge pull request #20 from poizan42/Support-set-column-collation
Browse files Browse the repository at this point in the history
Set collations on columns if defined by the model.
  • Loading branch information
PawelGerr authored Apr 27, 2022
2 parents a6bff0f + 9a5d71d commit d8aab67
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ private string GetColumnsDefinitions(SqlServerTempTableCreatorCacheKey options)
try
{
StoreObjectIdentifier? storeObject = null;
IEntityType? designTimeEntityType = null;

var isFirst = true;

Expand All @@ -232,8 +233,22 @@ private string GetColumnsDefinitions(SqlServerTempTableCreatorCacheKey options)
.Append(_sqlGenerationHelper.DelimitIdentifier(columnName)).Append(' ')
.Append(columnType);

if (options.UseDefaultDatabaseCollation && _stringColumnTypes.Any(t => columnType.StartsWith(t, StringComparison.OrdinalIgnoreCase)))
sb.Append(" COLLATE database_default");
if (_stringColumnTypes.Any(t => columnType.StartsWith(t, StringComparison.OrdinalIgnoreCase)))
{
// Collation information is not available from the runtime model, so we need to fetch it from the design time model
if (designTimeEntityType == null)
{
var designTimeModel = _ctx.GetService<IDesignTimeModel>().Model;
designTimeEntityType = designTimeModel.FindEntityType(property.Property.DeclaringEntityType.Name) ??
throw new InvalidOperationException($"Entity type {property.Property.DeclaringEntityType.Name} is missing from design time model.");
}
var designTimeEntityProperty = designTimeEntityType.GetProperty(property.Property.Name);
var collation = designTimeEntityProperty.GetCollation(storeObject.Value);
if (string.IsNullOrWhiteSpace(collation) && options.UseDefaultDatabaseCollation)
collation = "database_default";
if (!string.IsNullOrWhiteSpace(collation))
sb.Append(" COLLATE ").Append(collation);
}

sb.Append(property.Property.IsNullable ? " NULL" : " NOT NULL");

Expand Down

0 comments on commit d8aab67

Please sign in to comment.