c# - 将文档添加到 Azure Cosmos DB 时缺少属性

标签 c# azure .net-core azure-cosmosdb

我是 C# .net core 新手,正在尝试创建 azure 函数。

我正在从 stripe 包中扩展一个类 (BalanceTransaction)。

public class BalanceTransaction : StripeEntity<BalanceTransaction>, IHasId, IHasObject
{
    public BalanceTransaction();

    //
    // Summary:
    //     (Expanded) The Stripe object to which this transaction is related. For more information,
    //     see the expand documentation.
    [JsonIgnore]
    public IBalanceTransactionSource Source { get; set; }
    //
    // Summary:
    //     (ID of the IBalanceTransactionSource) The Stripe object to which this transaction
    //     is related.
    [JsonIgnore]
    public string SourceId { get; set; }
    //
    // Summary:
    //     Learn more about how reporting categories can help you understand balance transactions
    //     from an accounting perspective.
    [JsonProperty("reporting_category")]
    public string ReportingCategory { get; set; }
    //
    // Summary:
    //     Net amount of the transaction, in %s.
    [JsonProperty("net")]
    public long Net { get; set; }
    //
    // Summary:
    //     Detailed breakdown of fees (in %s) paid for this transaction.
    [JsonProperty("fee_details")]
    public List<BalanceTransactionFeeDetail> FeeDetails { get; set; }
    //
    // Summary:
    //     Fees (in %s) paid for this transaction.
    [JsonProperty("fee")]
    public long Fee { get; set; }
    //
    // Summary:
    //     If the transaction's net funds are available in the Stripe balance yet. Either
    //     available or pending.
    [JsonProperty("status")]
    public string Status { get; set; }
    //
    // Summary:
    //     The exchange rate used, if applicable, for this transaction. Specifically, if
    //     money was converted from currency A to currency B, then the amount in currency
    //     A, times exchange_rate, would be the amount in currency B. For example, suppose
    //     you charged a customer 10.00 EUR. Then the PaymentIntent's amount would be 1000
    //     and currency would be eur. Suppose this was converted into 12.34 USD in your
    //     Stripe account. Then the BalanceTransaction's amount would be 1234, currency
    //     would be usd, and exchange_rate would be 1.234.
    [JsonProperty("exchange_rate")]
    public decimal? ExchangeRate { get; set; }
    //
    // Summary:
    //     Three-letter ISO currency code, in lowercase. Must be a supported currency.
    [JsonProperty("currency")]
    public string Currency { get; set; }
    //
    // Summary:
    //     Time at which the object was created. Measured in seconds since the Unix epoch.
    [JsonConverter(typeof(UnixDateTimeConverter))]
    [JsonProperty("created")]
    public DateTime Created { get; set; }
    //
    // Summary:
    //     The date the transaction's net funds will become available in the Stripe balance.
    [JsonConverter(typeof(UnixDateTimeConverter))]
    [JsonProperty("available_on")]
    public DateTime AvailableOn { get; set; }
    //
    // Summary:
    //     Gross amount of the transaction, in %s.
    [JsonProperty("amount")]
    public long Amount { get; set; }
    //
    // Summary:
    //     String representing the object's type. Objects of the same type share the same
    //     value.
    [JsonProperty("object")]
    public string Object { get; set; }
    //
    // Summary:
    //     Unique identifier for the object.
    [JsonProperty("id")]
    public string Id { get; set; }
    //
    // Summary:
    //     An arbitrary string attached to the object. Often useful for displaying to users.
    [JsonProperty("description")]
    public string Description { get; set; }
    //
    // Summary:
    //     Transaction type: adjustment, advance, advance_funding, anticipation_repayment,
    //     application_fee, application_fee_refund, charge, connect_collection_transfer,
    //     contribution, issuing_authorization_hold, issuing_authorization_release, issuing_dispute,
    //     issuing_transaction, payment, payment_failure_refund, payment_refund, payout,
    //     payout_cancel, payout_failure, refund, refund_failure, reserve_transaction, reserved_funds,
    //     stripe_fee, stripe_fx_fee, tax_fee, topup, topup_reversal, transfer, transfer_cancel,
    //     transfer_failure, or transfer_refund. Learn more about balance transaction types
    //     and what they represent. If you are looking to classify transactions for accounting
    //     purposes, you might want to consider reporting_category instead. One of: adjustment,
    //     advance, advance_funding, anticipation_repayment, application_fee, application_fee_refund,
    //     charge, connect_collection_transfer, contribution, issuing_authorization_hold,
    //     issuing_authorization_release, issuing_dispute, issuing_transaction, payment,
    //     payment_failure_refund, payment_refund, payout, payout_cancel, payout_failure,
    //     refund, refund_failure, reserve_transaction, reserved_funds, stripe_fee, stripe_fx_fee,
    //     tax_fee, topup, topup_reversal, transfer, transfer_cancel, transfer_failure,
    //     or transfer_refund.
    [JsonProperty("type")]
    public string Type { get; set; }
}

我的扩展类包含两个额外的属性

public class CustomBalanceTransaction : BalanceTransaction
{
    [JsonPropertyName("customerid")]
    public string Customerid { get; set; }

    [JsonPropertyName("import_status")]
    public MiddlewareStatus ImportStatus { get; set; }
}

我的 azure 函数包含用于基于 CustomBalanceTransaction 类创建 azure cosmos db 文档的代码。

var customBalanceTransaction = new CustomBalanceTransaction
                        {
                            Id = balanceTransaction.Id,
                            Customerid = customer.Customerid,
                            ImportStatus = MiddlewareStatus.Received,
                            Source = balanceTransaction.Source,
                            Object = balanceTransaction.Object,
                            Amount = balanceTransaction.Amount,
                            AvailableOn = balanceTransaction.AvailableOn,
                            Created = balanceTransaction.Created,
                            Currency = balanceTransaction.Currency,
                            Description = balanceTransaction.Description,
                            ExchangeRate = balanceTransaction.ExchangeRate,
                            Fee = balanceTransaction.Fee,
                            FeeDetails = balanceTransaction.FeeDetails,
                            Net = balanceTransaction.Net,
                            ReportingCategory = balanceTransaction.ReportingCategory,
                            Status = balanceTransaction.Status,
                            Type = balanceTransaction.Type
                        };

                        var jsonString = JsonSerializer.Serialize(customBalanceTransaction);
                        var transaction = JsonSerializer.Deserialize<CustomBalanceTransaction>(jsonString);

                        log.LogInformation($"{jsonString}");

                        await transactionContainer.CreateItemAsync(transaction);```

When I serialise it to a string my json is correct and when I deserialise it, is correct. Whenever I insert it Azure cosmos DB it is missing my two properties.

json string in my console;
[![enter image description here][1]][1]
[1]: /image/3RHXs.png

Document in azure cosmos db
[![enter image description here][2]][2]
  [2]: /image/6Po0v.png

最佳答案

您在模型声明中混合了 Newtonsoft.Json 和 System.Text.Json。

基本类型使用 JsonProperty,其他类型使用 JsonPropertyName (System.Text.Json)。

由于 Cosmos DB V3 SDK 使用 Newtonsoft.Json 作为序列化引擎,请尝试让您的自定义类使用相同的装饰器:

public class CustomBalanceTransaction : BalanceTransaction
{
    [JsonProperty("customerid")]
    public string Customerid { get; set; }

    [JsonProperty("import_status")]
    public MiddlewareStatus ImportStatus { get; set; }
}

关于c# - 将文档添加到 Azure Cosmos DB 时缺少属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69976840/

相关文章:

azure - 如何从 Spring JMS 设置 Azure ServiceBus 的 ContentType

asp.net - 性能缓慢 Azure 'Web App' + Azure SQL DB

Azure 无法重新启动虚拟机 - NIC 未处于成功状态

c# - 将变量分配给数据表

c# - 动态组合算法

c# - MVVM Light - RaisePropertyChanged 不起作用

c# - Webresponse 对象出现 (500) 内部服务器错误

c# - 如何检查类型是否为类?

.net-core - 如何更改 Blazor PWA 应用程序刷新时出现的 "Authorising..."消息?

css - Blazor 突出显示选定的表格行