I'm trying to implement cascading deletes in this application which embeds RavenDB, via the CascadeDelete RavenDB bundle, but the bundle doesn't seem to be activated. How should I go about this?
In the below code snippet is a test console application that tries to enable cascading deletes in an embedded/in-memory RavenDB database, I have installed NuGet packages RavenDB.Embedded 2.5.2505-Unstable and RavenDB.Bundles.CascadeDelete 2.5.2505-Unstable for it:
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();
}
}
}
}
When you run this app, you should see that the child is not deleted with the parent, even though it's configured to through cascading delete. Please point out how I can make cascading deletes work in this program :)
Update:
I've tried adding the following after store.Initialize();
, but it makes no difference:
store.DocumentDatabase.Configuration.Catalog.Catalogs.Add(
new AssemblyCatalog(typeof(CascadeDeleteTrigger).Assembly));