Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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# RavenDB分片使更新充当插入_C#_Ravendb_Sharding - Fatal编程技术网

C# RavenDB分片使更新充当插入

C# RavenDB分片使更新充当插入,c#,ravendb,sharding,C#,Ravendb,Sharding,我正在尝试使用RavenDB为我的工作场所建立一个简单的概念证明。演示目前有2个执行基本循环策略的碎片。然后,作为故障切换,它又有两个碎片来复制这两个碎片中的每一个 我们检查并保存了各种业务记录。我们会得到ID,比如matt-businesss-35和bob-businesss-42,看起来不错。但是,当我们编辑一条记录时,它不会更新现有记录,而是执行插入操作。最常见的情况是,除了原来的Id之外,我们还得到了一个类似于matt-bob-business-42的Id 我们以本页为指南: 但是,我们

我正在尝试使用RavenDB为我的工作场所建立一个简单的概念证明。演示目前有2个执行基本循环策略的碎片。然后,作为故障切换,它又有两个碎片来复制这两个碎片中的每一个

我们检查并保存了各种业务记录。我们会得到ID,比如matt-businesss-35和bob-businesss-42,看起来不错。但是,当我们编辑一条记录时,它不会更新现有记录,而是执行插入操作。最常见的情况是,除了原来的Id之外,我们还得到了一个类似于matt-bob-business-42的Id

我们以本页为指南:

但是,我们通过替换DataDocumentstore.cs中的代码对其进行了分片修改:

var shards = new Dictionary<string, IDocumentStore>
                 {
                     {"bob", new DocumentStore() {Url = "http://bob:8080"}},
                     {"matt", new DocumentStore() {Url = "http://matt:8080"}},
                 };

var shardStrategy = new ShardStrategy(shards);

instance = new ShardedDocumentStore(shardStrategy);
instance.Conventions.IdentityPartsSeparator = "-";
instance.Initialize();
var shards=新字典
{
{“bob”,新文档库(){Url=”http://bob:8080"}},
{“matt”,新文档库(){Url=”http://matt:8080"}},
};
var shardStrategy=新的shardStrategy(碎片);
实例=新的ShardedDocumentStore(shardStrategy);
instance.Conventions.IdentityPartsSeparator=“-”;
初始化();
编辑操作如下所示:

public ActionResult Edit(string id)
{
    var model = DocumentSession.Load<Business>(id);
    return View(model);
}

[HttpPost]
public ActionResult Edit(string id, Business business)
{
    try
    {
        if (ModelState.IsValid)
        {
            DocumentSession.Store(business);
            return RedirectToAction("Index");
        }
        return View(business);
    }
    catch
    {
        return View();
    }
}
公共操作结果编辑(字符串id)
{
var模型=DocumentSession.Load(id);
返回视图(模型);
}
[HttpPost]
公共操作结果编辑(字符串id,业务)
{
尝试
{
if(ModelState.IsValid)
{
DocumentSession.商店(商务);
返回操作(“索引”);
}
返回视图(业务);
}
抓住
{
返回视图();
}
}
我们是不是搞错了什么事情来处理这些奇怪的问题?这似乎是一个相当简单的设置,但更新总是使用新的键名插入。

您的编辑操作是错误的。您需要先加载实体,然后将更改映射到加载的实例,而不是使用编辑的数据再次调用
.Store()
。RavenDBs会话自动跟踪该实例上的更改,并在调用
.SaveChanges()
时更新数据库中的文档

在您的特定情况下,我不知道您在哪里调用.SaveChanges,但我猜它在基本控制器上执行的OnAction或global.asax中的EndRequest中。两种方法都有效。您所要做的就是按id加载业务,更改其属性,就完成了。无需再次存储文档


AutoMapper可以帮助您使用Map()方法的重载更新实例,该方法接受预实例化的对象

我还没有让Automapper为我成功地映射对象,但是手动映射属性确实有效。