Giter VIP home page Giter VIP logo

documentdb-dotnet-todo-app's Introduction

page_type languages products description
sample
csharp
azure
dotnet
aspnet
azure-cosmos-db
This sample shows you how to use the Microsoft Azure Cosmos DB service to store and access data from an ASP.NET MVC application.

This repository has been archived. It uses the Azure Cosmos DB .NET SDK Version 2 which we have recently announced will be deprecated in August 2024. An updated version of this sample using the Azure Cosmos DB .NET SDK Version 3 can be found here.

Web application development with ASP.NET MVC using Azure Cosmos DB

This sample shows you how to use the Microsoft Azure Cosmos DB service to store and access data from an ASP.NET MVC application hosted on Azure Websites.

For a complete end-to-end walk-through of creating this application, please refer to the full tutorial on the Azure Cosmos DB documentation page.

Running this sample

  1. Before you can run this sample, you must have the following prerequisites:

2.Clone this repository using Git for Windows (http://www.git-scm.com/), or download the zip file.

3.From Visual Studio, open the todo.sln file from the root directory.

4.In Visual Studio Build menu, select Build Solution (or Press F6).

5.Retrieve the URI and PRIMARY KEY (or SECONDARY KEY) values from the Keys blade of your Azure Cosmos DB account in the Azure portal. For more information on obtaining endpoint & keys for your Azure Cosmos DB account refer to View, copy, and regenerate access keys and passwords.

If you don't have an account, see Create a database account to set one up.

6.In the Web.config file, located in the project root, find endpoint and authKey and replace the placeholder values with the values obtained for your account.

<add key="endpoint" value="~enter URI for your Azure Cosmos DB Account, from the Azure portal~" /> 
<add key="authKey" value="~enter either Primary or Secondary key for your Azure Cosmos DB Account, from the Azure portal~" /> 

7.You can now run and debug the application locally by pressing F5 in Visual Studio.

Deploy this sample to Azure

  1. In Visual Studio Solution Explorer, right-click on the project name and select Publish...

  2. Using the Publish Website dialog, select Microsoft Azure Web Apps

  3. In the next dialog, either select an existing web app, or follow the prompts to create a new web application. Note: If you choose to create a web application, the Web App Name chosen must be globally unique.

  4. Once you have selected the web app, click Publish

  5. After a short time, Visual Studio will complete the deployment and open a browser with your deployed application.

For additional ways to deploy this web application to Azure, please refer to the Deploy a web app in Azure App Service article which includes information on using Azure Resource Manager (ARM) Templates, Git, MsBuild, PowerShell, Web Deploy, and many more.

About the code

The code included in this sample is intended to get you going with a simple ASP.NET MVC application that connects to Azure Cosmos DB. It is not intended to be a set of best practices on how to build scalable enterprise grade web applications. This is beyond the scope of this quick start sample.

More information

ocument

documentdb-dotnet-todo-app's People

Contributors

acomsmpbot avatar aliuy avatar arramac avatar ealsur avatar joseph4tw avatar mimig1 avatar philpursglove avatar ryancrawcour avatar snehagunda avatar supernova-eng avatar v-thepet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

documentdb-dotnet-todo-app's Issues

Cross Partition Query Error

Hi Fellows - After downloading and executing the code, if any of you face the following issue apply the resolution as mentioned

Issue: Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception

Resolution: Add the parameter EnableCrossPartitionQuery = true to the following lines of code in your solution.
IDocumentQuery query = client.CreateDocumentQuery(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
.Where(predicate)
.AsDocumentQuery();

build fails

I am using Visual Studio 2017 15.7.5

$ dotnet --version
2.1.302

Build fails,

$ dotnet build
Microsoft (R) Build Engine version 15.7.179.6572 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Nothing to do. None of the projects specified contain packages to restore.
D:\git_ws\cosmosdb\documentdb-dotnet-todo-app\src\todo.csproj(230,5): error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\packages\Microsoft.Azure.DocumentDB.1.6.1\build\Microsoft.Azure.DocumentDB.targets.

Build FAILED.

D:\git_ws\cosmosdb\documentdb-dotnet-todo-app\src\todo.csproj(230,5): error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\packages\Microsoft.Azure.DocumentDB.1.6.1\build\Microsoft.Azure.DocumentDB.targets.
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.51

My environment variables in dev prompt looks like following,

**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.7.5
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************

D:\PFiles_x86\Microsoft Visual Studio\2017\Professional>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\atiqc\AppData\Roaming
ChocolateyInstall=D:\PFiles_x64\Chocolatey
ChocolateyLastPathUpdate=Thu Jun 28 19:06:16 2018
CommandPromptType=Native
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=ATIQ-RESEARCH
ComSpec=C:\WINDOWS\system32\cmd.exe
DevEnvDir=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\Common7\IDE\
DriverData=C:\Windows\System32\Drivers\DriverData
ExtensionSdkDir=C:\Program Files (x86)\Microsoft SDKs\Windows Kits\10\ExtensionSDKs
Framework40Version=v4.0
FrameworkDir=C:\Windows\Microsoft.NET\Framework\
FrameworkDir32=C:\Windows\Microsoft.NET\Framework\
FrameworkVersion=v4.0.30319
FrameworkVersion32=v4.0.30319
FSHARPINSTALLDIR=C:\Program Files (x86)\Microsoft SDKs\F#\10.1\Framework\v4.0\
HOMEDRIVE=C:
HOMEPATH=\Users\atiqc
INCLUDE=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.14.26428\include;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\ucrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\shared;C:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\winrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\cppwinrt
LIB=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.14.26428\lib\x86;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x86;C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\ucrt\x86;C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86;
LIBPATH=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.14.26428\lib\x86;D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.14.26428\lib\x86\store\references;C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17134.0;C:\Program Files (x86)\Windows Kits\10\References\10.0.17134.0;C:\Windows\Microsoft.NET\Framework\v4.0.30319;
LOCALAPPDATA=C:\Users\atiqc\AppData\Local
LOGONSERVER=\\ATIQ-RESEARCH
NETFXSDKDir=C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\
NUMBER_OF_PROCESSORS=8
OneDrive=C:\Users\atiqc\OneDrive
OS=Windows_NT
Path=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.14.26428\bin\HostX86\x86;D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.8;D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TestWindow;D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\bin\Roslyn;D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\;C:\Program Files (x86)\Microsoft SDKs\F#\10.1\Framework\v4.0\;C:\Program Files (x86)\Windows Kits\10\bin\10.0.17134.0\x86;C:\Program Files (x86)\Windows Kits\10\bin\x86;D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\\MSBuild\15.0\bin;C:\Windows\Microsoft.NET\Framework\v4.0.30319;D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\Common7\IDE\;D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\Common7\Tools\;C:\Program Files\Docker\Docker\Resources\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files\dotnet;C:\Program Files\Microsoft SQL Server\130\Tools\Binn;C:\WINDOWS\System32\OpenSSH;D:\PFiles_x64\choco\Node;C:\Program Files\dotnet\;;C:\Users\atiqc\.dotnet\tools
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=9e09
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\Users\atiqc\AppData\Local\Temp
TMP=C:\Users\atiqc\AppData\Local\Temp
UCRTVersion=10.0.17134.0
UniversalCRTSdkDir=C:\Program Files (x86)\Windows Kits\10\
USERDOMAIN=ATIQ-RESEARCH
USERDOMAIN_ROAMINGPROFILE=ATIQ-RESEARCH
USERNAME=atiqc
USERPROFILE=C:\Users\atiqc
VCIDEInstallDir=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\Common7\IDE\VC\
VCINSTALLDIR=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\VC\
VCToolsInstallDir=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.14.26428\
VCToolsRedistDir=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\VC\Redist\MSVC\14.14.26405\
VCToolsVersion=14.14.26428
VisualStudioVersion=15.0
VS150COMNTOOLS=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\Common7\Tools\
VSCMD_ARG_app_plat=Desktop
VSCMD_ARG_HOST_ARCH=x86
VSCMD_ARG_TGT_ARCH=x86
VSCMD_VER=15.7.5
VSINSTALLDIR=D:\PFiles_x86\Microsoft Visual Studio\2017\Professional\
windir=C:\WINDOWS
WindowsLibPath=C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17134.0;C:\Program Files (x86)\Windows Kits\10\References\10.0.17134.0
WindowsSdkBinPath=C:\Program Files (x86)\Windows Kits\10\bin\
WindowsSdkDir=C:\Program Files (x86)\Windows Kits\10\
WindowsSDKLibVersion=10.0.17134.0\
WindowsSdkVerBinPath=C:\Program Files (x86)\Windows Kits\10\bin\10.0.17134.0\
WindowsSDKVersion=10.0.17134.0\
WindowsSDK_ExecutablePath_x64=C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\x64\
WindowsSDK_ExecutablePath_x86=C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\
__DOTNET_ADD_32BIT=1
__DOTNET_PREFERRED_BITNESS=32
__VSCMD_PREINIT_PATH=C:\Program Files\Docker\Docker\Resources\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files\dotnet;C:\Program Files\Microsoft SQL Server\130\Tools\Binn;C:\WINDOWS\System32\OpenSSH;D:\PFiles_x64\choco\Node;C:\Program Files\dotnet\;;C:\Users\atiqc\.dotnet\tools

NullReferenceException: Object reference not set to an instance of an object.

I just tried "Quick-start" sample for .net Core and integrated in .NET Core 2.0 App and I am getting error as:
An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
Test.DocumentDBRepository+d__6.MoveNext() in DocumentDBRepository.cs, line 43

Note: The same code is working perfectly in .NET Core V1 with run-time 1.0.4

NullReferenceException: Object reference not set to an instance of an object.
Test.DocumentDBRepository+d__6.MoveNext() in DocumentDBRepository.cs
+
IDocumentQuery query = client.CreateDocumentQuery(
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Test.Controllers.ItemController+d__0.MoveNext() in ItemController.cs
+
var items = await DocumentDBRepository.GetItemsAsync(d => !d.Completed);
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d__10.MoveNext()

Code:

public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate)
        {
            IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
                UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),
                new FeedOptions { MaxItemCount = -1 })
                .Where(predicate)
                .AsDocumentQuery();
            List<T> results = new List<T>();
            while (query.HasMoreResults)
            {
                results.AddRange(await query.ExecuteNextAsync<T>());
            }

            return results;
        }

Unsupported ParitionKey value component

Hi, I found the outdated documentation on how to read an item from cosmos db by including the PartitionKey,

public static async Task<T> GetItemAsync(string id, string category)
 {
      await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id),new RequestOptions { PartitionKey = new PartitionKey(category) });

but what does the parameter id and category stand for?
Does the id parameter should be same as collection items.id? If that's the case why am I getting exception like Entity with the specified id does not exist in the system., as I can use the same value doing query search in SQL API as SELECT * FROM c WHERE c.id = "xxxx"

In addition, why would we need both value for id and partition key when retrieving a single item from collection? Why couldn't we just use id as it's unique key? If I don't know the partition key, am I unable to get the item now?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.