【问题标题】:Microsoft.oData.Client 7.1 posting only changed properties on nested objectsMicrosoft.oData.Client 7.1 仅在嵌套对象上发布更改的属性
【发布时间】:2023-01-05 18:12:59
【问题描述】:

我正在努力使用 Microsoft oData 客户端。我在我的 Visual Studio c#/.net Core 环境中生成了一个 Sap 业务单服务层 oData 客户端。 我需要在服务器上发布订单文档,包括文档(订单)行。但是,很明显,我不想发布具有所有空值的完整对象,而只发布属性集。这就是我创建 DataServiceCollection 以添加新订单和使用 context.SaveChanges(SaveChangesOptions.PostOnlySetProperties) 发布的原因。对于 Document 对象,仅发送更改的属性(根据需要),但嵌套对象(订单行)作为完整对象发送(具有所有空值,这是不希望的)。

我的代码看起来像这样:

var salesOrder = new Document{...//set required properties};
var dsc = new DataServiceCollection<Document>(context, nameof(context.Orders), null, null);
dsc.Add(salesOrder);
var orderLine = new DocumentLine{...//set required properties};
salesOrder.DocumentLines.Add(orderLine);
var response = await context.SaveChangesAsync(SaveChangesOptions.PostOnlySetProperties);

任何想法,如何强制客户端即使在嵌套对象/集合上也只发布更改的属性? 谢谢 RK。

【问题讨论】:

标签: .net odata


【解决方案1】:

更新:我不认为这是一个好的解决方案,它更像是一个 hack,但我已经通过从实体跟踪器中删除未使用的属性解决了这个问题:

serviceLayer.Configurations.RequestPipeline.OnEntryStarting((args) => EntryStarting(args));

void EntryStarting(WritingEntryArgs args)
{ 
    var usedPropertyNames = new List<string> { nameof(DocumentLine.ShipDate), nameof(DocumentLine.Quantity), nameof(DocumentLine.CostingCode), nameof(DocumentLine.DiscountPercent) };
            
    var usedProperties = args.Entry.Properties.Where(p => usedPropertyNames.Contains(p.Name)).ToList();
    args.Entry.Properties = usedProperties;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2019-12-14
    • 2020-07-11
    • 2014-02-24
    相关资源
    最近更新 更多