doug_w
doug_w

Reputation: 1370

RavenDB Includes still round-tripping to load included data

I have a parent/child relationship between a ProductFamily (the parent) and a list of subordinates (SaleItem). I am running the Ravendb Server locally with the server pulled up as a console app. When I query the Family data I am attempting to include the list of SaleItems in the session to avoid extra trips to the server. However on the console I see the subsequent calls to load the individual saleitem list for each family as I step through the foreach loop. I think I am doing something incorrectly and am puzzled as to what it may be. I am on day 2 of using RavenDB, so any handholding is appreciated.

Classes:

public class Family
    {
        public string Id { get { return string.Format(@"Families/{0}", this.FamilyID); } }
        public int FamilyID { get; set; }
        public string FamilyNumber { get; set; }
        public string Description { get; set; }
        public string[] SaleitemIds { get; set; }
        public override string ToString()
        {
            return string.Format("Number:{0}  -  {1}", FamilyNumber, Description);
        }
        [JsonIgnore]
        public List<SaleItem> SaleItems { get; set; }
    }
    public class SaleItem
    {
        public string Id { get { return string.Format(@"SaleItems/{0}", this.SaleItemID); } }
        public int SaleItemID { get; set; }
        public string Description { get; set; }
        public override string ToString()
        {
            return string.Format("Number:{0}  -  {1}", SaleItemID.ToString(), Description);
        }
    }

And the code:

List<SearchTerm> searchterms = new List<SearchTerm>(){ new SearchTerm(){term="1009110922"}
            ,new SearchTerm(){term="1009112439"}
            ,new SearchTerm(){term="1009122680"}
            ,new SearchTerm(){term="1009124177"}
            ,new SearchTerm(){term="1009133928"}
            ,new SearchTerm(){term="1009135435"}
            ,new SearchTerm(){term="1009148000"}};
        using (IDocumentSession session = documentStore.OpenSession())
        {
            var results = session.Query<Family>().Customize(o => o.Include<SaleItem>(s => s.Id)).Where(x => x.FamilyNumber.In(searchterms.Select(t => t.term).ToList()));

            foreach (Family fam in results)
            {
                Console.WriteLine(fam.ToString());

                fam.SaleItems = session.Load<SaleItem>(fam.SaleitemIds).ToList();
                foreach (SaleItem si in fam.SaleItems)
                {
                    Console.WriteLine("     " + si.ToString());
                }
            }
        }

As I step through the code I see the calls to Get the list of saleitems on the line:

fam.SaleItems = session.Load<SaleItem>(fam.SaleitemIds).ToList();

I believe I have implemented something incorrectly, but I am new enough with this platform to accept that I could have simply misunderstood what the behavior would be. There are definitely cases where I do not want the Saleitem doc to be embedded in the Family doc, so that is not really an option in this case.

Upvotes: 2

Views: 236

Answers (1)

Ayende Rahien
Ayende Rahien

Reputation: 22956

Doug_w, Look at what you are including:

  o.Include<SaleItem>(s => s.Id)

You probably want it to be:

  o.Include<SaleItem>(s => s.SaleitemIds )

Upvotes: 2

Related Questions