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

Commit c1fcb02

Browse files
committed
Fixed issue were SQL Server In-Memory tables require a nonclustered primary key.
1 parent de38574 commit c1fcb02

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServer2014OrmLiteDialectProvider.cs

Lines changed: 7 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,9 @@ public override string ToCreateTableStatement(Type tableType)
3739
if (columnDefinition == null)
3840
continue;
3941

42+
if (isMemoryTable && fieldDef.IsPrimaryKey)
43+
columnDefinition += " NONCLUSTERED";
44+
4045
if (sbColumns.Length != 0)
4146
sbColumns.Append(", \n ");
4247

@@ -54,10 +59,10 @@ public override string ToCreateTableStatement(Type tableType)
5459
sbConstraints.Append(GetForeignKeyOnUpdateClause(fieldDef.ForeignKey));
5560
}
5661

57-
if (tableType.HasAttribute<SqlServerMemoryOptimizedAttribute>())
62+
if (isMemoryTable)
5863
{
59-
sbMemOptimized.Append(" WITH (MEMORY_OPTIMIZED=ON");
6064
var attrib = tableType.FirstAttribute<SqlServerMemoryOptimizedAttribute>();
65+
sbMemOptimized.Append(" WITH (MEMORY_OPTIMIZED=ON");
6166
if (attrib.Durability == SqlServerDurability.SchemaOnly)
6267
sbMemOptimized.Append(", DURABILITY=SCHEMA_ONLY");
6368
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)