Skip to content

Instantly share code, notes, and snippets.

@anemitoff
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anemitoff/1be2a9f5743920dace98 to your computer and use it in GitHub Desktop.
Save anemitoff/1be2a9f5743920dace98 to your computer and use it in GitHub Desktop.
RavenDB Include by Dictionary Key or Values for Load Failing
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Client;
using Raven.Tests.Helpers;
using Xunit;
namespace Tests
{
public class DictionaryIncludeTest : RavenTestBase
{
public class Foo
{
public string Id { get; set; }
public Dictionary<string, string> BarIdInKey { get; set; }
public Dictionary<string, string> BarIdInValue { get; set; }
protected Foo() { }
public Foo(Bar[] bars)
{
BarIdInKey = bars.ToDictionary(b => b.Id, b=>"Some Value");
BarIdInValue = bars.ToDictionary(b => "Some Key", b => b.Id);
}
}
public class Bar
{
public Bar() { }
public string Id { get; set; }
public string Title { get; set; }
}
private dynamic MakeAndStoreEntities(IDocumentStore db)
{
using (var session = db.OpenSession())
{
var bar = new Bar();
session.Store(bar);
var foo = new Foo(new[] {bar});
session.Store(foo);
session.SaveChanges();
return new {foo, bar};
}
}
[Fact]
public void Can_include_dictionary_key()
{
var db = NewDocumentStore(); db.Initialize();
var entities = MakeAndStoreEntities(db);
var session = db.OpenSession();
var loaded = session.Include<Foo>(f => f.BarIdInKey.Keys).Load<Foo>(entities.foo.Id);
Assert.NotNull(loaded);
var bar = session.Load<Bar>(entities.bar.Id);
Assert.NotNull(bar);
// The following fails because NumberOfRequests is 2
Assert.Equal(1, session.Advanced.NumberOfRequests);
}
[Fact]
public void Can_include_dictionary_value()
{
var db = NewDocumentStore(); db.Initialize();
var entities = MakeAndStoreEntities(db);
var session = db.OpenSession();
var foo = session.Include<Foo>(f => f.BarIdInValue.Values.Select(x=>x)).Load<Foo>(entities.foo.Id);
Assert.NotNull(foo);
var bar = session.Load<Bar>(entities.bar.Id);
Assert.NotNull(bar);
// The following fails because NumberOfRequests is 2
Assert.Equal(1, session.Advanced.NumberOfRequests);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment