Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 避免Azure表存储中的新upsert_C#_Azure_Upsert_Azure Table Storage_Azure Storage - Fatal编程技术网

C# 避免Azure表存储中的新upsert

C# 避免Azure表存储中的新upsert,c#,azure,upsert,azure-table-storage,azure-storage,C#,Azure,Upsert,Azure Table Storage,Azure Storage,Steve Marx在这里介绍了在Azure表存储中执行升级的新扩展方法,作为新存储协议版本的一部分: 但是,如果我想执行无条件合并或抛出的原始操作,而不是upsert,该怎么办。我想合并一个对象,更新一个字段,但如果实体不存在则抛出,而不是创建一个只包含我要合并的属性的新实体 这可能吗?请注意,我想在其他地方使用upsert,因此我已经开始让IoC向我提供从GetDataServiceContext2011创建的上下文,而不是GetDataServiceContext。我想我可以在两者之间交

Steve Marx在这里介绍了在Azure表存储中执行升级的新扩展方法,作为新存储协议版本的一部分:

但是,如果我想执行无条件合并或抛出的原始操作,而不是upsert,该怎么办。我想合并一个对象,更新一个字段,但如果实体不存在则抛出,而不是创建一个只包含我要合并的属性的新实体

这可能吗?请注意,我想在其他地方使用upsert,因此我已经开始让IoC向我提供从
GetDataServiceContext2011
创建的上下文,而不是
GetDataServiceContext
。我想我可以在两者之间交替使用,但当Azure团队更新官方库时,这不会有帮助

根据:

插入或合并实体操作使用合并谓词,并且必须 使用2011-08-18或更新版本调用。此外,它没有 使用If匹配头。这些属性区分此操作 从更新实体操作,尽管请求主体是相同的 对于这两种操作


那么,如何让存储库在保存时发出通配符
If Match
,而不是在Match时根本不发出通配符呢?

只需使用带有星号的etag的
附件即可。如果匹配:
,则会产生一个
。下面是一个完整的工作示例:

class Entity : TableServiceEntity
{
    public string Text { get; set; }
    public Entity() { }
    public Entity(string rowkey) : base(string.Empty, rowkey) { }
}
class Program
{
    static void Update(CloudStorageAccount account)
    {
        var ctx = account.CreateCloudTableClient().GetDataServiceContext();

        var entity = new Entity("foo") { Text = "bar" };
        ctx.AttachTo("testtable", entity, "*");
        ctx.UpdateObject(entity);
        ctx.SaveChangesWithRetries();
    }

    static void Main(string[] args)
    {
        var account = CloudStorageAccount.Parse(args[0]);
        var tables = account.CreateCloudTableClient();
        tables.CreateTableIfNotExist("testtable");
        var ctx = tables.GetDataServiceContext();

        try { Update(account); } catch (Exception e) { Console.WriteLine("Exception (as expected): " + e.Message); }

        ctx.AddObject("testtable", new Entity("foo") { Text = "foo" });
        ctx.SaveChangesWithRetries();

        try { Update(account); } catch (Exception e) { Console.WriteLine("Unexpected exception: " + e.Message); }

        Console.WriteLine("Now text is: " + tables.GetDataServiceContext().CreateQuery<Entity>("testtable").Where(e => e.PartitionKey == string.Empty && e.RowKey == "foo").Single().Text);
        tables.DeleteTableIfExist("testtable");
    }
}
类实体:表服务实体
{
公共字符串文本{get;set;}
公共实体(){}
公共实体(string rowkey):基(string.Empty,rowkey){}
}
班级计划
{
静态无效更新(CloudStorageAccount)
{
var ctx=account.CreateCloudTableClient().GetDataServiceContext();
var entity=新实体(“foo”){Text=“bar”};
ctx.AttachTo(“测试表”,实体“*”);
ctx.UpdateObject(实体);
ctx.SaveChangesWithRetries();
}
静态void Main(字符串[]参数)
{
var account=CloudStorageAccount.Parse(args[0]);
var tables=account.CreateCloudTableClient();
tables.CreateTableIfNotExist(“testtable”);
var ctx=tables.GetDataServiceContext();
尝试{Update(account);}catch(Exception e){Console.WriteLine(“Exception(如预期的那样):”+e.Message);}
AddObject(“testtable”,新实体(“foo”){Text=“foo”});
ctx.SaveChangesWithRetries();
尝试{Update(account);}catch(Exception e){Console.WriteLine(“意外异常:+e.Message”);}
Console.WriteLine(“现在的文本是:+tables.GetDataServiceContext().CreateQuery(“testtable”)。其中(e=>e.PartitionKey==string.Empty&&e.RowKey==foo”).Single().text);
tables.DeleteTableIfExist(“测试表”);
}
}

谢谢。在这件事上我有点傻-P@smarxetag实际上做什么?我试图用谷歌搜索它,但没有找到任何解释。@starcom在这种情况下,问题本身给出了答案并提供了来源(MSDN)。