简体   繁体   English

C# for loop dynamodb add item 只添加一行

[英]C# for loop dynamodb add item just add one row

I have a problem that for loop to add item我有一个问题,for 循环添加项目
I see aws table just add one row after finished我看到 aws 表在完成后只添加一行
I try so many way but they doesn't work我尝试了很多方法但它们不起作用

class Program : Base
{
    static void Main(string[] args)
    {
        stock_group stock_group = new stock_group();
        stock_group.default_insert();
    }
}
public async Task<bool> default_insert()
{
    List<tb_stock_group> data = JsonConvert.DeserializeObject<tb_stock_group>>(@"
        [{""id"":1,""name"":""水泥工業""},
        {""id"":2,""name"":""食品工業""},
        {""id"":3,""name"":""塑膠工業""}]
        ");

    for (int i = 0; i < data.Count; i++)
    {
        var response = await SaveToDynamoDB(data[i]);
        Console.WriteLine(response);
    }

    Console.WriteLine("success!");
    return false;
}

public async Task<T> SaveToDynamoDB<T>(T obj) where T :class
{
    DynamoDBContext context = new DynamoDBContext(Get_Client());
    await context.SaveAsync(obj);
    var response = await context.LoadAsync<T>(obj);

    return response;
}

In all likelihood, the program is exiting before most of the calls to DynamoDB happen.很可能,程序在对 DynamoDB 的大部分调用发生之前就退出了。 This is because you are making a call to an async function from code that is not async .这是因为您正在从非async代码中调用async函数。 If you change the code in static Main to the following it should work:如果您将static Main的代码更改为以下内容,它应该可以工作:

class Program : Base
{
    static void Main(string[] args)
    {
        stock_group stock_group = new stock_group();
        stock_group.default_insert().Wait();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM