我正在使用 Azure Functions 并且想要使用强制绑定 https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#imperative-bindings到我的 Cosmos DB 存储库。
起初我在中定义了我的输出参数Run
-method:
[DocumentDB("TablesDB", "minified-urls", ConnectionStringSetting = "CosmosConnectionString", CreateIfNotExists = true)] out MinifiedUrl minifiedUrl,
这很好用。
但我宁愿使用命令式绑定,这样我就可以动态定义绑定中使用的值。
我尝试的第一件事是这个,但这不起作用,因为你需要设置ConnectionStringSetting
.
var output = await binder.BindAsync<MinifiedUrl>(new DocumentDBAttribute("TablesDB", "minified-urls"));
因此,将这段绑定更改为以下内容应该可行,至少我是这么认为的。
var output = await binder.BindAsync<MinifiedUrl>(new DocumentDBAttribute("TablesDB", "minified-urls")
{
CreateIfNotExists = true,
ConnectionStringSetting = "CosmosConnectionString"
});
当你这样做时,活页夹会抱怨缺少Id
财产,如果你填写这个Id
您还需要设置属性PartitionKey
.
所以最后这就是我想出来的。
var output = await binder.BindAsync<MinifiedUrl>(new DocumentDBAttribute("TablesDB", "minified-urls")
{
CreateIfNotExists = true,
ConnectionStringSetting = "CosmosConnectionString",
Id = Guid.NewGuid().ToString(),
PartitionKey = DateTime.UtcNow.Year.ToString()
});
因为我想在存储库中创建一个新文档,活页夹无法找到某些内容,因此output
变量是null
.
那么,我想知道的是如何进行?声明性绑定工作正常,但我不知道如何让这个命令工作。