简体   繁体   中英

Azure C# Cosmos DB: Required property 'id' missing when inserting data

I am creating a program that writes data into a Cosmos DB ( OneAuthZDeltaRoleDatabase ), however, when I write data, I receive the following error:

"The input content is invalid because the required properties - 'id; ' - are missing"

Below is my code where I programmatically create the Cosmos DB ( OneAuthZDeltaRoleDatabase ) and its container ( OneAuthZDeltaRoleContainer ), and attempt to insert the data into the Cosmos DB (the database and the container are successfully created, but inserting data is unsuccessful):

    // 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));
            }
     }

在此处输入图像描述

This is the code for the DeltaAPIMapping class (the class I am adding to the 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; }
    }
}

You need the JsonProperty name to be "id", not "AppId". If AppId is what your app needs, then try flipping these and trying again.

I had the same issue and I had changed my property from string to Guid and now it works.

class DeltaAPIMapping
    {
        [JsonProperty(PropertyName = "id")]
        public Guid MyIdWithAnotherName { get; set; }

The important part is: Your document has to have an id Property, this "id" is going to store a Guid value, If it doesn't exist (I mean a property with that name) then you may use the Json Annotations to setup any Guid property as a Json Property with the name "id", this annotation is going to map all the references to "id" (from the JsonDocument) and store them in "MyIdWithAnotherName", this is how it works in my example.

Additionally, maybe if you use a string instead of Guid is going to work too but I have not tested that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM