我正在尝试通过CascadeDelete RavenDB 包在嵌入 RavenDB 的这个应用程序中实现级联删除,但该包似乎没有被激活。我该怎么办?
在下面的代码片段中是一个测试控制台应用程序,它尝试在嵌入式/内存中 RavenDB 数据库中启用级联删除,我已经安装了 NuGet 包 RavenDB.Embedded 2.5.2505-Unstable 和 RavenDB.Bundles.CascadeDelete 2.5.2505-Unstable为了它:
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));