Azure Functions 表绑定:如何更新一行? [英] Azure Functions Table Binding: How do I update a row?

查看:19
本文介绍了Azure Functions 表绑定:如何更新一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于 Azure 函数更新 Azure 表中的一行.我看到 Table 绑定可以处理一个 ICollector,它有一个 Add 方法,可以添加一行.我还看到您使用 IQueryable 来读取数据.

I am trying to update a row in an Azure table based on an Azure Function. I see that the Table bindings can handle an ICollector which has an Add method which will add a row. I also see that you use an IQueryable to read the data.

如何更新数据中的特定行?

How do you go about updating a specific row in the data?

我在 WebJobs 中看到了与 InsertOrReplace 相关的内容,它是 TableOperations 的一种方法,但我不知道它是否或如何发挥作用,以及如何将它与 Azure Functions 结合使用.

I saw in WebJobs something related to InsertOrReplace which is a method of TableOperations but I don't know if or how that comes into play and how to use it with Azure Functions.

推荐答案

以下是您可以执行此操作的一种方法.在我们的下一个版本中,这些步骤将变得更容易,但现在您需要手动引入 Azure 存储 SDK.

Following is one way you can do this. These steps will get easier with our next release, but for now you need to manually bring in the Azure Storage SDK.

首先,按照此帮助页面 用于引入 Azure 存储 SDK.您将上传一个看起来像这样的 project.json 到您的函数文件夹:

First, follow the steps in the "Package Management" section of this help page to pull in the Azure Storage SDK. You'll be uploading a project.json that looks like this to your function folder:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "WindowsAzure.Storage": "7.0.0"
      }
    }
   }
}

注意:在下一个版本中,我们将自动包含 Azure 存储 SDK,因此您可以直接在代码中使用它.拉入包后,您可以在集成选项卡选项卡高级编辑器中输入如下所示的函数元数据:

Note: In the next release we'll automatically include the Azure Storage SDK so you can just use it directly in your code. After you've pulled in the package, you can enter function metadata like the following in the Integrate tab tab Advanced Editor:

{
  "bindings": [
    {
      "name": "input",
      "type": "manualTrigger",
      "direction": "in"
    },
    {
      "name": "table",
      "type": "table",
      "tableName": "test",
      "connection": "<your connection>",
      "direction": "in"
    }
  ]
}

下面是对应的代码.我们在这里绑定了一个 CloudTable,它允许我们读/写实体:

And below is the corresponding code. We're binding to a CloudTable here which lets us read/write entities:

#r "Microsoft.WindowsAzure.Storage"

using System;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;

public static void Run(string input, CloudTable table, TraceWriter log)
{
    TableOperation operation = TableOperation.Retrieve<Person>("AAA", "001");
    TableResult result = table.Execute(operation);
    Person person = (Person)result.Result;

    log.Verbose($"{person.Name} is {person.Status}");

    person.Status = input;
    operation = TableOperation.Replace(person);
    table.Execute(operation);
}

public class Person : TableEntity
{
    public string Name   { get;set; }
    public string Status { get;set; }
}

我在此示例中使用了 ManualTrigger,但表绑定将适用于您拥有的任何触发器.通过上述设置,我可以在门户的运行输入框中输入一个值并点击运行.该函数将查询实体,输出其当前值,然后使用我的输入进行更新.

I used a ManualTrigger for this example, but the table binding will work with whatever trigger you have. With the above set up, I can enter a value in the Run input box of the portal and hit run. The function will query the entity, output its current values, then update using my input.

其他排列也是可能的.例如,如果您有一个来自另一个绑定参数的实体实例,您可以以类似的方式使用 CloudTable 来更新它.

Other permutations are possible. For example, if you have an entity instance from another binding parameter, you can use the CloudTable in a similar way to update it.

这篇关于Azure Functions 表绑定:如何更新一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆