2017-08-02 44 views
3

我有一个使用DocumentDB属性连接到Cosmos数据库的Azure函数。我正在使用Visual Studio 2017工具的Azure函数。下面是简单的功能在Azure函数的DocumentDB属性中发送SqlQuery

[FunctionName("DocumentDbGet")] 
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log, 
     [DocumentDB("FunctionJunctionDemo", "Demo")]IEnumerable<DemoModel> items) 
    { 
     //Can perform operations on the "items" variable, which will be the result of the table/collection you specify 
     string name = req.GetQueryNameValuePairs() 
      .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0) 
      .Value; 

     var item = items.Where(x => x.FirstName == name); 
     return req.CreateResponse(HttpStatusCode.OK); 
    } 

我希望能够传递一个SqlQueryDocumentDB属性的参数,像这样的一个:

[FunctionName("DocumentDbGet")] 
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log, 
     [DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = $"select * from c where c.Id = {Id}")]IEnumerable<DemoModel> items) 
    { 
     //Can perform operations on the "items" variable, which will be the result of the table/collection you specify 
     string name = req.GetQueryNameValuePairs() 
      .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0) 
      .Value; 

     var item = items.Where(x => x.FirstName == name); 
     return req.CreateResponse(HttpStatusCode.OK); 
    } 

我只看到1例做到这一点,据报道,它本该工作。 https://github.com/Azure/Azure-Functions/issues/271 我收到的“错误”是它不能识别名为SqlQuery的任何可能参数。

我看过Azure函数输入绑定文档 https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-with-multiple-documents,它显示function.json文件中包含sqlQuery属性的输出。那到底是怎么回事?

如果不可能在DocumentDB属性中传入SqlQuery,那么最好的做法是事先过滤结果以避免返回整个集合,然后通过LINQ查询运行它?

回答

5

您需要参考1.1.0-beta版本的Microsoft.Azure.WebJobs.Extensions.DocumentDB NuGet包(或更高版本)。

在该版本中SqlQueryDocumentDB属性的有效参数。您的代码编译对我来说,如果我以前select字符串中删除$标志:

[DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = "select * from c where c.Id = {Id}")] 

你不需要$ - 它用于串插在C#中,不是你想在这里做一些事情。

+0

Hi @Mikhail - 感谢您的快速回答!所以,即使我删除了从一开始就不需要的'$',它仍然不认为它是有效的。也许我有一个版本不匹配?我正在使用Microsoft.Azure.WebJobs.Extensions.DocumentDB的v1.0.0,这是'DocumentDB'属性的来源。也许我需要更新我的Azure CLI? –

+0

,因此我将Azure函数CLI更新为最新版本,以及Visual Studio 2017 Preview to Preview 7,然后允许我将Visual Studio的Azure函数工具从0.2更新为0.3.30802。这些都没有帮助。在我的机器上仍然在'SqlQuery'下得到了令人敬畏的红色曲线。也许我完全错过了一个包? –

+1

@EricFleming我引用'... Extensions.DocumentDB'包的'1.1.0-beta1'版本。这能改变它吗? – Mikhail