简体   繁体   English

Azure表存储(Partition key row key),如何在同一个partition不同rowKey对应插入多个实体?

[英]Azure table storage (Partition key row key), how to insert multiple entities in corresponding to same parttion and different rowKey?

I am a C# .net developer and I am creating an email tracking system.我是一名 C# .net 开发人员,我正在创建一个电子邮件跟踪系统。 I want to store my data to the Azure Table Storage, but I want to create all the entities in the same partition with different row key.我想将我的数据存储到 Azure 表存储,但我想使用不同的行键在同一分区中创建所有实体。 My properties key are same but the value is different.我的属性键相同,但值不同。 For example:例如:

partition key = "Test+id"
row key=123
properties:
subject:"Hello",
from:"xyz@gmail.com",
to:"abc@gmail.com",
body:"Hello I am a test email"

Now I want to create a copy of above, but with different rowKey value, same Partition key-value and same property key but with different value.现在我想创建上面的副本,但具有不同的 rowKey 值、相同的 Partition 键值和相同的属性键但具有不同的值。 Like this:像这样:

partition key = "Test+id",
row key="787",
properties:
subject: "HelloTesting",
from:"sam@gmail.com",
to:"alex@gmail.com",
body: "Hello I am a test email2, this is so nice"

Here is the C# code that I am using to add the properties:这是我用来添加属性的 C# 代码:

foreach (KeyValuePair<string, string> keyValuePair in list)
{
    dynamicTableEntity.RowKey = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.SSS");
    Console.WriteLine(keyValuePair.Key);

    if (keyValuePair.Key.Equals("subject"))
    {
        dynamicTableEntity.Properties.Add("subject", EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
    }
    else if (keyValuePair.Key.Equals("toRecipients") )
    {
        dynamicTableEntity.Properties.Add("toRecipients", EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
    }
    else if (keyValuePair.Key.Equals("from") )
    {
        dynamicTableEntity.Properties.Add("from", EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
    }
    else if (keyValuePair.Key.Equals("bodyPreview") )
    {
        dynamicTableEntity.Properties.Add("bodyPreview", EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
    }
    else
    {
        dynamicTableEntity.Properties.Add(keyValuePair.Key, EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
    }
}

Any help would be appreciated.任何帮助,将不胜感激。

Your code of inserting entity worked for me.您插入实体的代码对我有用。 Just to keep it simple, as you know the data type:只是为了简单起见,因为您知道数据类型:

foreach (KeyValuePair<string, string> keyValuePair in list)
{
    if (keyValuePair.Key.Equals("subject"))
    {
        dynamicTableEntity.Properties.Add("subject", EntityProperty.GeneratePropertyForString(keyValuePair.Value));
    }

    ...
}

As you didn't mention what exception you get, I assume there's a RowKey conflict when the row keys get the same value in one second as "SSS" used in DateTime has no effect.由于你没有提到你得到了什么异常,我假设当行键在一秒内获得相同的值时存在RowKey冲突,因为 DateTime 中使用的“SSS”无效。 Try using fff or FFF to get milliseconds .尝试使用fffFFF来获取毫秒

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

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