我正在创建一个将数据写入Cosmos DB(OneAuthZDeltaRoleDatabase
)的程序,但是,当我写入数据时,出现以下错误:
"The input content is invalid because the required properties - 'id; ' - are missing"
下面是我的代码,在其中我以编程方式创建Cosmos DB(OneAuthZDeltaRoleDatabase
)及其容器(OneAuthZDeltaRoleContainer
),并尝试将数据插入Cosmos DB(数据库和容器已成功完成)创建,但无法成功插入数据):
// Creates the Cosmos DB database where we store the last delta API call for each app
private static async Task CreateDeltaAPICallDatabaseAsync()
{
// Create a new database
lastAPICallDatabase = await cosmosClient.CreateDatabaseIfNotExistsAsync(lastDeltaDatabaseId);
}
// Creates the Cosmos DB container where we store the last delta API for each app
private static async Task CreateDeltaAPICallContainerAsync()
{
// Create a new container
lastAPICallContainer = await lastAPICallDatabase.CreateContainerIfNotExistsAsync(lastDeltaContainerId, "/AppId");
}
/* Timer function that reads the role assignments table (OneAuthZRoleAssignments) on a configurable schedule
* and computes + displays a mapping of users to their corresponding role assignments (e.x. every 2 mins) */
[FunctionName("InitiateChangeMonitoring")]
public static async Task InitiateChangeMonitoring([TimerTrigger("%TimerTriggerPeriod%")] TimerInfo myTimer, ILogger log)
{
// Create the necessary Cosmos DB infastructure
await CreateDeltaAPICallDatabaseAsync();
await CreateDeltaAPICallContainerAsync();
foreach (string appId in oneAuthZAppIds)
{
// Initialize the base delta API call timestamp for that app id (if it does not exist)
await lastAPICallContainer.CreateItemAsync(new DeltaAPIMapping(appId, baselineSnapshotTime), new PartitionKey(appId));
}
}
这是DeltaAPIMapping
类(我要添加到Cosmos DB中的类)的代码。
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace AccessChangeMonitoring
{
class DeltaAPIMapping
{
[JsonProperty(PropertyName = "AppId")]
public string id { get; set; }
public DeltaAPIMapping(string appId, DateTime callTimestamp)
{
this.id = Guid.NewGuid().ToString();
this.appId = appId;
this.callTimestamp = callTimestamp;
}
public string appId { get; set; }
public DateTime callTimestamp { get; set; }
}
}
答案 0 :(得分:0)
您需要JsonProperty名称为“ id”,而不是“ AppId”。如果您的应用需要AppId,请尝试将其翻转然后重试。
答案 1 :(得分:0)
我遇到了同样的问题,我已将我的属性从字符串更改为 Guid,现在它可以工作了。
class DeltaAPIMapping
{
[JsonProperty(PropertyName = "id")]
public Guid MyIdWithAnotherName { get; set; }
重要的部分是: 你的文档必须有一个 id 属性,这个“id”将存储一个 Guid 值, 如果它不存在(我的意思是具有该名称的属性),那么您可以使用 Json 注释将任何 Guid 属性设置为名为“id”的 Json 属性,此注释会将所有引用映射到“id” "(来自 JsonDocument)并将它们存储在“MyIdWithAnotherName”中,这就是我的示例中的工作方式。
此外,也许如果您使用字符串而不是 Guid 也可以工作,但我还没有测试过。