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

Commit 886068c

Browse files
committed
Merge pull request #386 from Connect-Develop/master
Allow Nullable AutoIncrementId
2 parents 1ddcaf8 + 29fa0f9 commit 886068c

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

src/ServiceStack.OrmLite/OrmLiteWriteExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ internal static int Save<T>(this IDbCommand dbCmd, params T[] objs)
602602
internal static bool Save<T>(this IDbCommand dbCmd, T obj)
603603
{
604604
var id = obj.GetId();
605-
var existingRow = dbCmd.SingleById<T>(id);
605+
var existingRow = id != null ? dbCmd.SingleById<T>(id) : default(T);
606606
if (Equals(existingRow, default(T)))
607607
{
608608
var modelDef = typeof(T).GetModelDefinition();
@@ -636,7 +636,8 @@ internal static int SaveAll<T>(this IDbCommand dbCmd, IEnumerable<T> objs)
636636
var firstRow = saveRows.FirstOrDefault();
637637
if (Equals(firstRow, default(T))) return 0;
638638

639-
var defaultIdValue = firstRow.GetId().GetType().GetDefaultValue();
639+
var firstRowId = firstRow.GetId();
640+
var defaultIdValue = firstRowId != null ? firstRowId.GetType().GetDefaultValue() : null;
640641

641642
var idMap = defaultIdValue != null
642643
? saveRows.Where(x => !defaultIdValue.Equals(x.GetId())).ToSafeDictionary(x => x.GetId())

tests/ServiceStack.OrmLite.Tests/OrmLiteSaveTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public void Save_populates_AutoIncrementId()
2727
db.Save(row);
2828

2929
Assert.That(row.Id, Is.Not.EqualTo(0));
30+
Assert.That(row.Id, Is.Not.Null);
3031
}
3132
}
3233

@@ -58,6 +59,56 @@ public void SaveAll_populates_AutoIncrementId()
5859
}
5960
}
6061

62+
[Test]
63+
public void Save_populates_NullableAutoIncrementId()
64+
{
65+
using (var db = OpenDbConnection())
66+
{
67+
db.CreateTable<PersonWithNullableAutoId>(overwrite: true);
68+
69+
var row = new PersonWithNullableAutoId
70+
{
71+
FirstName = "Jimi",
72+
LastName = "Hendrix",
73+
Age = 27
74+
};
75+
76+
db.Save(row);
77+
78+
Assert.That(row.Id, Is.Not.EqualTo(0));
79+
}
80+
}
81+
82+
[Test]
83+
public void SaveAll_populates_NullableAutoIncrementId()
84+
{
85+
using (var db = OpenDbConnection())
86+
{
87+
db.CreateTable<PersonWithNullableAutoId>(overwrite: true);
88+
89+
var rows = new[] {
90+
new PersonWithNullableAutoId {
91+
FirstName = "Jimi",
92+
LastName = "Hendrix",
93+
Age = 27
94+
},
95+
new PersonWithNullableAutoId {
96+
FirstName = "Kurt",
97+
LastName = "Cobain",
98+
Age = 27
99+
},
100+
};
101+
102+
db.Save(rows);
103+
104+
Assert.That(rows[0].Id, Is.Not.EqualTo(0));
105+
Assert.That(rows[0].Id, Is.Not.Null);
106+
Assert.That(rows[1].Id, Is.Not.EqualTo(0));
107+
Assert.That(rows[1].Id, Is.Not.Null);
108+
Assert.That(rows[0].Id, Is.Not.EqualTo(rows[1].Id));
109+
}
110+
}
111+
61112
[Test]
62113
public void Save_works_within_a_transaction()
63114
{

tests/ServiceStack.OrmLite.Tests/Shared/Person.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ public class PersonWithAutoId
3737
public int Age { get; set; }
3838
}
3939

40+
public class PersonWithNullableAutoId
41+
{
42+
[AutoIncrement]
43+
public int? Id { get; set; }
44+
public string FirstName { get; set; }
45+
public string LastName { get; set; }
46+
public int Age { get; set; }
47+
}
48+
4049
public class EntityWithId
4150
{
4251
public int Id { get; set; }

0 commit comments

Comments
 (0)