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

Commit 9c3721d

Browse files
committed
Merge pull request #389 from tvjames/nullable-datetimeoffset-poco
Fix insert/update of Nullable DateTimeOffset in POCO
2 parents 9f88298 + 30495cf commit 9c3721d

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public override string GetQuotedValue(object value, Type fieldType)
212212
protected override object GetValueOrDbNull<T>(FieldDefinition fieldDef, object obj)
213213
{
214214
var value = GetValue<T>(fieldDef, obj);
215-
if (fieldDef.FieldType == typeof(DateTimeOffset))
215+
if (fieldDef.FieldType == typeof(DateTimeOffset) && value != null)
216216
{
217217
var dateTimeOffsetValue = (DateTimeOffset)value;
218218
return dateTimeOffsetValue.ToString("o");

tests/ServiceStack.OrmLite.Sqlite.Windows.Tests/ServiceStack.OrmLite.Sqlite.Windows.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Link>UseCase\SimpleUseCase.cs</Link>
6868
</Compile>
6969
<Compile Include="Properties\AssemblyInfo.cs" />
70+
<Compile Include="UseCase\NullableDateTimeOffset.cs" />
7071
</ItemGroup>
7172
<ItemGroup>
7273
<None Include="packages.config" />
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.IO;
5+
using NUnit.Framework;
6+
using ServiceStack.OrmLite.Sqlite;
7+
using ServiceStack.DataAnnotations;
8+
9+
namespace ServiceStack.OrmLite.Tests.UseCase
10+
{
11+
[TestFixture]
12+
public class NullableDateTimeOffset
13+
{
14+
[TestFixtureSetUp]
15+
public void TestFixtureSetUp()
16+
{
17+
//Inject your database provider here
18+
OrmLiteConfig.DialectProvider = new SqliteOrmLiteDialectProvider();
19+
}
20+
21+
public class Record
22+
{
23+
[AutoIncrement]
24+
public long Id { get; set; }
25+
26+
public DateTimeOffset CreatedDate { get; set; }
27+
28+
public DateTimeOffset? ModifiedDate { get; set; }
29+
}
30+
31+
[Test]
32+
public void Can_Insert_RecordWithNullable_DateTimeOffset()
33+
{
34+
var path = Config.SqliteFileDb;
35+
if (File.Exists(path))
36+
File.Delete(path);
37+
38+
using (IDbConnection db = path.OpenDbConnection())
39+
{
40+
db.CreateTable<OrmLite.Tests.UseCase.NullableDateTimeOffset.Record>(true);
41+
42+
Assert.DoesNotThrow(() => db.Insert(new OrmLite.Tests.UseCase.NullableDateTimeOffset.Record() { Id = 1, CreatedDate = DateTime.Now }));
43+
}
44+
45+
File.Delete(path);
46+
}
47+
48+
[Test]
49+
public void Can_Update_RecordWithNullable_DateTimeOffset()
50+
{
51+
var path = Config.SqliteFileDb;
52+
if (File.Exists(path))
53+
File.Delete(path);
54+
55+
using (IDbConnection db = path.OpenDbConnection())
56+
{
57+
db.CreateTable<OrmLite.Tests.UseCase.NullableDateTimeOffset.Record>(true);
58+
59+
db.Insert(new OrmLite.Tests.UseCase.NullableDateTimeOffset.Record() {Id = 1, CreatedDate = DateTime.Now });
60+
61+
var record = db.LoadSingleById<OrmLite.Tests.UseCase.NullableDateTimeOffset.Record>(1);
62+
record.ModifiedDate = DateTimeOffset.Now;
63+
64+
Assert.DoesNotThrow(() => db.Update(record));
65+
}
66+
67+
File.Delete(path);
68+
}
69+
70+
[Test]
71+
public void Can_UpdateWithNull_RecordWithNullable_DateTimeOffset()
72+
{
73+
var path = Config.SqliteFileDb;
74+
if (File.Exists(path))
75+
File.Delete(path);
76+
77+
using (IDbConnection db = path.OpenDbConnection())
78+
{
79+
db.CreateTable<OrmLite.Tests.UseCase.NullableDateTimeOffset.Record>(true);
80+
81+
db.Insert(new OrmLite.Tests.UseCase.NullableDateTimeOffset.Record() { Id = 1, CreatedDate = DateTime.Now, ModifiedDate = DateTimeOffset.Now });
82+
83+
var record = db.LoadSingleById<OrmLite.Tests.UseCase.NullableDateTimeOffset.Record>(1);
84+
record.ModifiedDate = null;
85+
86+
Assert.DoesNotThrow(() => db.Update(record));
87+
}
88+
89+
File.Delete(path);
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)