将文档添加到 Azure Cosmos DB 时缺少属性
Missing properties when adding document to Azure Cosmos DB
我是 C# .net 核心的新手,我正在尝试创建 azure 函数。
我正在从 stripe 包扩展一个 class (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; }
}
我的扩展 class 包含两个额外的属性
public class CustomBalanceTransaction : BalanceTransaction
{
[JsonPropertyName("customerid")]
public string Customerid { get; set; }
[JsonPropertyName("import_status")]
public MiddlewareStatus ImportStatus { get; set; }
}
我的 azure 函数包含基于 CustomBalanceTransaction class.
创建 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]: https://i.stack.imgur.com/3RHXs.png
Document in azure cosmos db
[![enter image description here][2]][2]
[2]: https://i.stack.imgur.com/6Po0v.png
您在模型声明中混用了 Newtonsoft.Json 和 System.Text.Json。
基本类型正在使用 JsonProperty
,您的其他类型正在使用 JsonPropertyName
(System.Text.Json)。
由于 Cosmos DB V3 SDK 使用 Newtonsoft.Json 作为序列化引擎,请尝试让您的自定义 class 使用相同的装饰器:
public class CustomBalanceTransaction : BalanceTransaction
{
[JsonProperty("customerid")]
public string Customerid { get; set; }
[JsonProperty("import_status")]
public MiddlewareStatus ImportStatus { get; set; }
}
我是 C# .net 核心的新手,我正在尝试创建 azure 函数。
我正在从 stripe 包扩展一个 class (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; }
}
我的扩展 class 包含两个额外的属性
public class CustomBalanceTransaction : BalanceTransaction
{
[JsonPropertyName("customerid")]
public string Customerid { get; set; }
[JsonPropertyName("import_status")]
public MiddlewareStatus ImportStatus { get; set; }
}
我的 azure 函数包含基于 CustomBalanceTransaction class.
创建 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]: https://i.stack.imgur.com/3RHXs.png
Document in azure cosmos db
[![enter image description here][2]][2]
[2]: https://i.stack.imgur.com/6Po0v.png
您在模型声明中混用了 Newtonsoft.Json 和 System.Text.Json。
基本类型正在使用 JsonProperty
,您的其他类型正在使用 JsonPropertyName
(System.Text.Json)。
由于 Cosmos DB V3 SDK 使用 Newtonsoft.Json 作为序列化引擎,请尝试让您的自定义 class 使用相同的装饰器:
public class CustomBalanceTransaction : BalanceTransaction
{
[JsonProperty("customerid")]
public string Customerid { get; set; }
[JsonProperty("import_status")]
public MiddlewareStatus ImportStatus { get; set; }
}