我正在尝试通过CascadeDelete RavenDB软件包在这个嵌入RavenDB的应用程序中实现级联删除,但是这个软件包似乎没有被激活。我应该怎么做呢?
在下面的代码片段中是一个测试控制台应用程序,它试图在嵌入式/内存RavenDB数据库中启用级联删除,我已经安装了NuGet包RavenDB.Embedded 2.5.2505-Unstable和RavenDB.Bundles.CascadeDelete 2.5。 2505-不稳定:
using System;
using System.Linq;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Json.Linq;
namespace ConsoleApplication1
{
public class Parent
{
public string Id { get; set; }
}
public class Child
{
public string Id { get; set; }
public string ParentId { get; set; }
}
public static class RavenExtensions
{
public static void AddCascadeDeleteReference(this IAdvancedDocumentSessionOperations session,
object entity, params string[] documentKeys)
{
var metadata = session.GetMetadataFor(entity);
if (metadata == null)
throw new InvalidOperationException(
"The entity must be tracked in the session before calling this method.");
if (documentKeys.Length == 0)
throw new ArgumentException(
"At least one document key must be specified.");
const string metadataKey = "Raven-Cascade-Delete-Documents";
RavenJToken token;
if (!metadata.TryGetValue(metadataKey, out token))
token = new RavenJArray();
var list = (RavenJArray)token;
foreach (var documentKey in documentKeys.Where(key => !list.Contains(key)))
list.Add(documentKey);
metadata[metadataKey] = list;
}
}
class Program
{
static void Main(string[] args)
{
var store = new EmbeddableDocumentStore { RunInMemory = true };
store.Initialize();
Parent parent;
using (var session = store.OpenSession())
{
parent = new Parent();
session.Store(parent);
session.SaveChanges();
var child = new Child();
child.ParentId = parent.Id;
session.Store(child);
session.Advanced.AddCascadeDeleteReference(parent, child.Id);
session.SaveChanges();
}
using (var session = store.OpenSession())
{
Console.WriteLine("Before deleting parent:");
foreach (var child in session.Query<Child>())
{
Console.WriteLine(" Child: {0} of parent {1}", child.Id, child.ParentId);
}
parent = session.Load<Parent>(parent.Id);
session.Delete(parent);
session.SaveChanges();
Console.WriteLine("After deleting parent:");
foreach (var child in session.Query<Child>())
{
Console.WriteLine(" Child: {0} of parent {1}", child.Id, child.ParentId);
}
Console.WriteLine("Press any key...");
Console.ReadKey();
}
}
}
}
当您运行此应用程序时,您应该看到子项未被父项删除,即使它已配置为通过级联删除。请指出我如何在此程序中进行级联删除:)
更新
我尝试在store.Initialize();
之后添加以下内容,但它没有任何区别:
store.DocumentDatabase.Configuration.Catalog.Catalogs.Add(
new AssemblyCatalog(typeof(CascadeDeleteTrigger).Assembly));
答案 0 :(得分:2)
由于您处于嵌入模式,因此您有两种选择:
您可以将bundle dll复制到plugins目录中。
您可以引用捆绑包dll,然后手动将其添加到目录中:
documentStore.DocumentDatabase.Configuration.Catalog.Catalogs
.Add(new AssemblyCatalog(typeof(CascadeDeleteTrigger).Assembly));
尽管不要同时做。如果你选择#2选项,那么bundle dll不应该在plugins文件夹中,否则它会被拾取两次。
答案 1 :(得分:0)
这有点违反直觉,但我发现我需要将packages\RavenDB.Bundles.CascadeDelete.2.5.2505-Unstable\lib\net40\Raven.Bundles.CascadeDelete.dll
复制到ConsoleApplication1\Plugins\Raven.Bundles.CascadeDelete.dll
并配置Visual Studio将文件复制到输出目录,以便RavenDB接收它。
现在,CascadeDelete捆绑包正常工作,并且子目录已与父目录一起删除。