c# - 使用 Ravendb 批量删除

标签 c# ravendb

我正在尝试使用以下逻辑从 RavenDB 中的集合中删除文档 ID

    var documentstore = new DocumentStore
{
    Url = "http://localhost:8080",

    DefaultDatabase = "Employee"
};
documentstore.Initialize();
using (var session = documentstore.OpenSession())
{

    var test = new List<string>();

    test.Add("emps/81993");
    test.Add("emps/40319");

    var w1 = session.Load<Employee>(test);
    session.Delete(w1);
    session.SaveChanges();
}

我得到以下错误

Models.Employee[] is not associated with the session, cannot delete unknown

如何从集合中批量删除文档 ID?

谢谢

最佳答案

您正在尝试删除员工数组,而不是每个员工本身。当您在 Load 中传入一个 Enumerable 时,您将得到一个包含每个实体的数组。

试试这个:

using (var session = documentstore.OpenSession())
{
    var test = new List<string>();

    test.Add("emps/81993");
    test.Add("emps/40319");

    Employee[] employees = session.Load<Employee>(test);
    foreach (var employee in employees)
    {
        session.Delete(employee);
    }

    session.SaveChanges();
}

为了进一步解释,返回的数组不被 RavenDb 的 UoW 跟踪,但数组中的每个单独的项目都是,这就是为什么你得到关于 Employee[] 的消息与 session 无关的原因。

关于c# - 使用 Ravendb 批量删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28993163/

相关文章:

c# - EventStore 和 RavenDB 持久化 JsonReaderException

c# - 使用反射类型查询 RavenDB

RavenDB租户加载缓慢

c# - 转换为日期时间 : how to set format

c# - 使用 JSON.NET 将属性反序列化为 ExpandoObject

c# - fluent nhibernate 的大问题,c# 和 MySQL 需要在 BLOB 中搜索

c# - 通用列表中的 Orderby

c# - 在 Azure Function 中使用相关 ID 进行日志记录

asp.net-mvc-3 - 另一个数据库实例 RavenDB MVC3 已使用的临时路径

node.js - Ravendb Nodejs,在 map 索引中使用Enums