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

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

问题描述

我正在尝试基于Azure函数更新Azure表中的行.我看到表绑定可以处理具有添加方法的ICollector,该方法将添加一行.我还看到您使用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.

首先,请按照

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函数表绑定:如何更新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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