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

Commit 5330012

Browse files
authored
Merge pull request #545 from KevinHoward/master
Fixed issue were SQL Server In-Memory tables require a non-clustered primary key.
2 parents de38574 + ccea1fe commit 5330012

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServer2014OrmLiteDialectProvider.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public override string ToCreateTableStatement(Type tableType)
1616
var sbConstraints = StringBuilderCacheAlt.Allocate();
1717
var sbMemOptimized = StringBuilderCacheAlt.Allocate();
1818

19+
var isMemoryTable = tableType.HasAttribute<SqlServerMemoryOptimizedAttribute>();
20+
1921
var modelDef = OrmLiteUtils.GetModelDefinition(tableType);
2022
foreach (var fieldDef in modelDef.FieldDefinitions)
2123
{
@@ -37,6 +39,23 @@ public override string ToCreateTableStatement(Type tableType)
3739
if (columnDefinition == null)
3840
continue;
3941

42+
var collationAttribs = fieldDef.PropertyInfo.GetAttributes<SqlServerCollateAttribute>();
43+
if (collationAttribs.Count > 0)
44+
{
45+
columnDefinition += $" COLLATE {collationAttribs[0].collation}";
46+
}
47+
48+
if (isMemoryTable && fieldDef.IsPrimaryKey)
49+
{
50+
columnDefinition = columnDefinition.Replace("PRIMARY KEY", "PRIMARY KEY NONCLUSTERED");
51+
52+
var bucketCountAtribs = fieldDef.PropertyInfo.GetAttributes<SqlServerBucketCountAttribute>();
53+
if (bucketCountAtribs.Count > 0)
54+
{
55+
columnDefinition += $" HASH WITH (BUCKET_COUNT={bucketCountAtribs[0].count})";
56+
}
57+
}
58+
4059
if (sbColumns.Length != 0)
4160
sbColumns.Append(", \n ");
4261

@@ -54,10 +73,10 @@ public override string ToCreateTableStatement(Type tableType)
5473
sbConstraints.Append(GetForeignKeyOnUpdateClause(fieldDef.ForeignKey));
5574
}
5675

57-
if (tableType.HasAttribute<SqlServerMemoryOptimizedAttribute>())
76+
if (isMemoryTable)
5877
{
59-
sbMemOptimized.Append(" WITH (MEMORY_OPTIMIZED=ON");
6078
var attrib = tableType.FirstAttribute<SqlServerMemoryOptimizedAttribute>();
79+
sbMemOptimized.Append(" WITH (MEMORY_OPTIMIZED=ON");
6180
if (attrib.Durability == SqlServerDurability.SchemaOnly)
6281
sbMemOptimized.Append(", DURABILITY=SCHEMA_ONLY");
6382
else if (attrib.Durability == SqlServerDurability.SchemaAndData)

src/ServiceStack.OrmLite.SqlServerTests/MemoryOptimizedAttributeTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void CanCreateMemoryOptimizedTableWithSchemaAndDurability()
5050
[SqlServerMemoryOptimized]
5151
public class TypeWithMemTableNoDurability
5252
{
53-
[AutoIncrement]
53+
[PrimaryKey]
5454
public int Id { get; set; }
5555

5656
public string Name { get; set; }
@@ -59,7 +59,7 @@ public class TypeWithMemTableNoDurability
5959
[SqlServerMemoryOptimized(SqlServerDurability.SchemaOnly)]
6060
public class TypeWithMemTableSchemaOnlyDurability
6161
{
62-
[AutoIncrement]
62+
[PrimaryKey]
6363
public int Id { get; set; }
6464

6565
public string Name { get; set; }
@@ -68,7 +68,7 @@ public class TypeWithMemTableSchemaOnlyDurability
6868
[SqlServerMemoryOptimized(SqlServerDurability.SchemaAndData)]
6969
public class TypeWithMemTableSchemaAndDataDurability
7070
{
71-
[AutoIncrement]
71+
[PrimaryKey]
7272
public int Id { get; set; }
7373

7474
public string Name { get; set; }

0 commit comments

Comments
 (0)