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

Commit c43b4c7

Browse files
committed
Fix loading null references on self reference POCO's
1 parent 3d5e8de commit c43b4c7

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

lib/ServiceStack.Text.dll

0 Bytes
Binary file not shown.

lib/ServiceStack.Text.pdb

0 Bytes
Binary file not shown.

src/ServiceStack.OrmLite/OrmLiteReadExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ internal static List<Into> LoadListWithReferences<Into, From>(this IDbCommand db
10581058
{
10591059
object childResult;
10601060
var fkValue = refSelf.GetValue(result);
1061-
if (map.TryGetValue(fkValue, out childResult))
1061+
if (fkValue != null && map.TryGetValue(fkValue, out childResult))
10621062
{
10631063
fieldDef.SetValueFn(result, childResult);
10641064
}

tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public void Can_SaveAllReferences_then_Load_them()
384384
}
385385

386386
[Test]
387-
public void Can_save_with_null_references()
387+
public void Can_save_and_load_with_null_references()
388388
{
389389
var customer = new Customer
390390
{
@@ -396,6 +396,34 @@ public void Can_save_with_null_references()
396396
db.Save(customer, references: true);
397397

398398
Assert.That(customer.Id, Is.GreaterThan(0));
399+
400+
var dbCustomer = db.LoadSingleById<Customer>(customer.Id);
401+
Assert.That(dbCustomer.Name, Is.EqualTo("Customer 1"));
402+
403+
var dbCustomers = db.LoadSelect<Customer>(q => q.Id == customer.Id);
404+
Assert.That(dbCustomers.Count, Is.EqualTo(1));
405+
Assert.That(dbCustomers[0].Name, Is.EqualTo("Customer 1"));
406+
}
407+
408+
[Test]
409+
public void Can_save_and_load_self_references_with_null_references()
410+
{
411+
var customer = new SelfCustomer
412+
{
413+
Name = "Customer 1",
414+
PrimaryAddress = null,
415+
};
416+
417+
db.Save(customer, references: true);
418+
419+
Assert.That(customer.Id, Is.GreaterThan(0));
420+
421+
var dbCustomer = db.LoadSingleById<SelfCustomer>(customer.Id);
422+
Assert.That(dbCustomer.Name, Is.EqualTo("Customer 1"));
423+
424+
var dbCustomers = db.LoadSelect<SelfCustomer>(q => q.Id == customer.Id);
425+
Assert.That(dbCustomers.Count, Is.EqualTo(1));
426+
Assert.That(dbCustomers[0].Name, Is.EqualTo("Customer 1"));
399427
}
400428

401429
[Test]

0 commit comments

Comments
 (0)