Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c1460f0

Browse files
committed
Add COLLATE to SqlServerOrmLiteDialectProvider column definition.
1 parent ee33f4d commit c1460f0

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Data;
44
using System.Data.SqlClient;
55
using System.IO;
6+
using ServiceStack.DataAnnotations;
67
using ServiceStack.OrmLite.SqlServer.Converters;
78
using ServiceStack.Text;
89
#if NETSTANDARD1_3
@@ -200,11 +201,43 @@ public override string ToChangeColumnNameStatement(Type modelType, FieldDefiniti
200201

201202
public override string GetColumnDefinition(FieldDefinition fieldDef)
202203
{
204+
// https://msdn.microsoft.com/en-us/library/ms182776.aspx
203205
if (fieldDef.IsRowVersion)
204206
return $"{fieldDef.FieldName} rowversion NOT NULL";
205207

206-
var definition = base.GetColumnDefinition(fieldDef);
207-
return definition;
208+
var fieldDefinition = fieldDef.CustomFieldDefinition ??
209+
GetColumnTypeDefinition(fieldDef.ColumnType, fieldDef.FieldLength, fieldDef.Scale);
210+
211+
var sql = StringBuilderCache.Allocate();
212+
sql.Append($"{GetQuotedColumnName(fieldDef.FieldName)} {fieldDefinition}");
213+
214+
if (fieldDef.IsPrimaryKey)
215+
{
216+
sql.Append(" PRIMARY KEY");
217+
if (fieldDef.AutoIncrement)
218+
{
219+
sql.Append(" ").Append(AutoIncrementDefinition);
220+
}
221+
}
222+
else
223+
{
224+
sql.Append(fieldDef.IsNullable ? " NULL" : " NOT NULL");
225+
}
226+
227+
// https://msdn.microsoft.com/en-us/library/ms184391.aspx
228+
var collation = fieldDef.PropertyInfo.FirstAttribute<SqlServerCollateAttribute>()?.Collation;
229+
if (!string.IsNullOrEmpty(collation))
230+
{
231+
sql.Append($" COLLATE {collation}");
232+
}
233+
234+
var defaultValue = GetDefaultValue(fieldDef);
235+
if (!string.IsNullOrEmpty(defaultValue))
236+
{
237+
sql.AppendFormat(DefaultValueFormat, defaultValue);
238+
}
239+
240+
return StringBuilderCache.ReturnAndFree(sql);
208241
}
209242

210243
public override string ToSelectStatement(ModelDefinition modelDef,

0 commit comments

Comments
 (0)