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

Commit 77588ee

Browse files
committed
Merge branch 'master' of github.com:ServiceStack/ServiceStack.OrmLite
2 parents 69caf68 + 9c14985 commit 77588ee

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
@@ -58,6 +58,57 @@ public void SaveAll_populates_AutoIncrementId()
5858
}
5959
}
6060

61+
[Test]
62+
public void Save_populates_NullableAutoIncrementId()
63+
{
64+
using (var db = OpenDbConnection())
65+
{
66+
db.CreateTable<PersonWithNullableAutoId>(overwrite: true);
67+
68+
var row = new PersonWithNullableAutoId
69+
{
70+
FirstName = "Jimi",
71+
LastName = "Hendrix",
72+
Age = 27
73+
};
74+
75+
db.Save(row);
76+
77+
Assert.That(row.Id, Is.Not.EqualTo(0));
78+
Assert.That(row.Id, Is.Not.Null);
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)