【发布时间】:2021-01-21 19:51:33
【问题描述】:
问题:
我正在开发一个使用 Cosmos DB 作为我的持久性存储的 .NET Core 5.0 Web 应用程序,当枚举设置为 0(默认值)时,它似乎没有持久化枚举。在下面的代码中,当我创建会话时,默认的 SessionStatus 值为 Planned。如果我将 Session 设置为 InProgress 或 Completed。它在数据库中分别显示为 1 或 2。
我的代码 会话类
public class Session
{
[JsonProperty("creator_id")]
public string CreatorId { get; private set; }
[JsonProperty("session_status")]
public SessionStatus SessionStatus { get; private set; }
}
public enum SessionStatus
{
Planned,
InProgress,
Completed
}
回购:
var document = await cosmosDbClient.CreateDocumentAsync(session);
CosmosClient:
public async Task<Document> CreateDocumentAsync(object document, RequestOptions options = null,
bool disableAutomaticIdGeneration = false, CancellationToken cancellationToken = default(CancellationToken))
{
return await _documentClient.CreateDocumentAsync(
UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), document, options,
disableAutomaticIdGeneration, cancellationToken);
}
我尝试过的:
- 我已尝试设置枚举 json 在枚举对象上转换为在 db 中存储为字符串,但在 db 中 session_status 的行为相同。
- 我可以将 SessionStatus 的默认值设置为 1 作为解决方法,但我更愿意理解底层问题。
【问题讨论】:
-
听起来您使用的 JSON 序列化程序设置为忽略默认值。您可以在那里检查选项,或者如果您只想为此属性设置它,请添加属性
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]。 -
成功了!!不知道为什么它不会首先设置为包含......
标签: c# .net-core azure-cosmosdb azure-cosmosdb-sqlapi asp.net-core-5.0