Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DocumentDB / CosmosDB - Entity with the specified id does not exist in the system

i tried to connect documentdb from my asp.net website but i am getting this error.

Entity with the specified id does not exist in the system

DocumentClientException: Entity with the specified id does not exist in the system

code as follows in aspx code behind

protected async void Page_Load(object sender, EventArgs e)
{
    Response.Write("Page Load<br/>");
    await GetData();
}

public async Task GetData()
{
    try
    {
        Response.Write("<br/> Get Data function Start<br/><br/>");
        using (var client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"]))
        {
            //await client.OpenAsync();
            RequestOptions reqOpt = new RequestOptions { PartitionKey = new PartitionKey(209) };
            var parameters = new dynamic[] { 1 };
            StoredProcedureResponse<object> result = await client.ExecuteStoredProcedureAsync<object>(
                UriFactory.CreateStoredProcedureUri(ConfigurationManager.AppSettings["database"], ConfigurationManager.AppSettings["pcsd"], "GetMemberbyId"), reqOpt, parameters);
            Response.Write(result.Response.ToString());
        }
        Response.Write("<br/><br/> Get Data function End");
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

stored procedure as follows

function GetMemberbyId(memId) {
    var collection = getContext().getCollection();

    //return getContext().getResponse().setBody('no docs found');
    // Query documents and take 1st item.
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT * FROM root m where m.memberId='+memId,
        function (err, feed, options) {
            if (err) throw err;

            // Check the feed and if empty, set the body to 'no docs found', 
            // else take 1st element from feed
            if (!feed || !feed.length) getContext().getResponse().setBody('no docs found');
            else getContext().getResponse().setBody(feed);
        });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

in my localhost it's working fine but website published to azure web apps and running i am getting above error

like image 537
Chenchu Krishna Giddaluru Avatar asked May 01 '17 17:05

Chenchu Krishna Giddaluru


People also ask

How do I connect my Cosmos DB?

Sign in to Azure portal. From All resources, find and navigate to your Azure Cosmos DB account, select Keys, and copy the Primary Connection String. Go to https://cosmos.azure.com/, paste the connection string and select Connect.

Can I use Entity Framework with Cosmos DB?

This database provider allows Entity Framework Core to be used with Azure Cosmos DB. The provider is maintained as part of the Entity Framework Core Project. It is strongly recommended to familiarize yourself with the Azure Cosmos DB documentation before reading this section.


1 Answers

I just spent a couple of hours troubleshooting this, only to find that I had firewalled my instance to a point where I could not connect locally. Keep in mind that the Azure portal document query will obviously still work even when you have no direct access via the API / C# client.

Try setting the firewall to allow All Networks temporarily to check access.

CosmosDB Firewall Settings

like image 52
Murray Foxcroft Avatar answered Nov 12 '22 15:11

Murray Foxcroft