使用 PopulateWithNonDefaultValues 进行的部分更新会覆盖子 类 中的空字段

Partial updates with PopulateWithNonDefaultValues overwrites null fields in sub classes

我有一个 class "company",其中包含一个子 class "address"。 "Address" 包含一个字段 "city" 和一个字段 "postalcode"。两者都可为空(字符串)。

我有一家现有公司,地址 class 中的两个字段均已填充。我想要做的是使用公司对象更新邮政编码字段,其中仅填充了地址 class 中的一个字段。

结果不是我想要的。目标对象中的 "city" 字段与 "postalcode".

一起被覆盖

在我看来,它取代了地址字段,而不仅仅是 class 中填充的字段。

有人知道如何解决这个问题吗?

原始对象:

 "name": "Some company",
 "address": {
        "city": "New York",
        "postalCode": "1234"
    }

更新使用:

 "name": null,
 "address": {
        "city": null,
        "postalCode": "1122"
    }

结束于:

 "name": "Some company",
 "address": {
        "city": null,
        "postalCode": "1122"
    }

使用 Auto Mapping PopulateWithNonDefaultValues() 扩展方法将 替换 地址 属性 如果它不为空,这就是结果地址结束的原因使用更新的地址,即它不合并两个地址,它用更新的非空地址替换它。

如果您想合并地址,则需要单独进行,例如:

original.Address.PopulateWithNonDefaultValues(updated.Address);
updated.Address = null; // skip populating Address
original.PopulateWithNonDefaultValues(updated);