@@ -15,46 +15,74 @@ public override string GetColumnDefinition(FieldDefinition fieldDef)
15
15
return $ "{ fieldDef . FieldName } rowversion NOT NULL";
16
16
17
17
var fieldDefinition = fieldDef . CustomFieldDefinition ??
18
- GetColumnTypeDefinition ( fieldDef . ColumnType , fieldDef . FieldLength , fieldDef . Scale ) ;
18
+ GetColumnTypeDefinition ( fieldDef . ColumnType , fieldDef . FieldLength , fieldDef . Scale ) ;
19
+
20
+ var memTableAttrib = fieldDef . PropertyInfo . ReflectedType . FirstAttribute < SqlServerMemoryOptimizedAttribute > ( ) ;
21
+ var isMemoryTable = memTableAttrib != null ;
19
22
20
23
var sql = StringBuilderCache . Allocate ( ) ;
21
24
sql . Append ( $ "{ GetQuotedColumnName ( fieldDef . FieldName ) } { fieldDefinition } ") ;
22
25
23
- if ( fieldDef . IsPrimaryKey )
26
+ if ( fieldDef . FieldType == typeof ( string ) )
24
27
{
25
- var isMemoryTable = fieldDef . PropertyInfo . DeclaringType . FirstAttribute < SqlServerMemoryOptimizedAttribute > ( ) != null ;
28
+ // https://msdn.microsoft.com/en-us/library/ms184391.aspx
29
+ var collation = fieldDef . PropertyInfo . FirstAttribute < SqlServerCollateAttribute > ( ) ? . Collation ;
30
+ if ( ! string . IsNullOrEmpty ( collation ) )
31
+ {
32
+ sql . Append ( $ " COLLATE { collation } ") ;
33
+ }
34
+ }
35
+
36
+ var bucketCount = fieldDef . PropertyInfo . FirstAttribute < SqlServerBucketCountAttribute > ( ) ? . Count ;
26
37
38
+ if ( fieldDef . IsPrimaryKey )
39
+ {
27
40
if ( isMemoryTable )
28
41
{
29
- sql . Append ( fieldDef . IsNullable ? " NULL" : " NOT NULL" ) ;
30
- sql . Append ( " PRIMARY KEY NONCLUSTERED" ) ;
31
-
32
- var bucketCount = fieldDef . PropertyInfo . FirstAttribute < SqlServerBucketCountAttribute > ( ) ? . Count ;
33
- if ( bucketCount . HasValue )
34
- {
35
- sql . Append ( $ " HASH WITH (BUCKET_COUNT = { bucketCount . Value } )") ;
36
- }
42
+ sql . Append ( $ " NOT NULL PRIMARY KEY NONCLUSTERED") ;
37
43
}
38
44
else
39
45
{
40
46
sql . Append ( " PRIMARY KEY" ) ;
47
+
48
+ if ( fieldDef . IsNonClustered )
49
+ sql . Append ( " NONCLUSTERED" ) ;
41
50
}
42
51
43
52
if ( fieldDef . AutoIncrement )
44
53
{
45
54
sql . Append ( " " ) . Append ( AutoIncrementDefinition ) ;
46
55
}
56
+
57
+ if ( isMemoryTable && bucketCount . HasValue )
58
+ {
59
+ sql . Append ( $ " HASH WITH (BUCKET_COUNT = { bucketCount . Value } )") ;
60
+ }
47
61
}
48
- else
62
+ else if ( ! isMemoryTable && fieldDef . IsUnique )
49
63
{
50
- sql . Append ( fieldDef . IsNullable ? " NULL" : " NOT NULL" ) ;
51
- }
64
+ sql . Append ( " UNIQUE" ) ;
52
65
53
- // https://msdn.microsoft.com/en-us/library/ms184391.aspx
54
- var collation = fieldDef . PropertyInfo . FirstAttribute < SqlServerCollateAttribute > ( ) ? . Collation ;
55
- if ( ! string . IsNullOrEmpty ( collation ) )
66
+ if ( fieldDef . IsNonClustered )
67
+ sql . Append ( " NONCLUSTERED" ) ;
68
+ }
69
+ else
56
70
{
57
- sql . Append ( $ " COLLATE { collation } ") ;
71
+ if ( isMemoryTable && bucketCount . HasValue )
72
+ {
73
+ sql . Append ( $ " NOT NULL INDEX { GetQuotedColumnName ( "IDX_" + fieldDef . FieldName ) } ") ;
74
+
75
+ if ( fieldDef . IsNonClustered )
76
+ {
77
+ sql . Append ( " NONCLUSTERED" ) ;
78
+ }
79
+
80
+ sql . Append ( $ " HASH WITH (BUCKET_COUNT = { bucketCount . Value } )") ;
81
+ }
82
+ else
83
+ {
84
+ sql . Append ( fieldDef . IsNullable ? " NULL" : " NOT NULL" ) ;
85
+ }
58
86
}
59
87
60
88
var defaultValue = GetDefaultValue ( fieldDef ) ;
0 commit comments