1

I'm attempting to implement a DataContractSerializer to generate an XML file that represents our somewhat complicated object model. The root object has multiple ICollection properties, one of which has multiple ICollection properties, multiples of which have multiple ICollection properties... you get the idea.

I decorated all my relevant classes with [DataContract(Name = "foo")] tags, read this question about using Include(), and started framing it out. As I put together the top layer, I wondered if the second layer would need explicit declarations as well. Here's what I have so far:

    public void Serialize(string DataCode)
    {
        Product prod = context.Products
            .Include(p => p.Products)
            .Include(p => p.References)
            .Include(p => p.Dates)
            .Include(p => p.Weights)
            .Include(p => p.Notes)
            .Include(p => p.Rules)  // Rules have PriceConditions, which have Prices...
            .Include(p => p.DataBooks)
            .First(m => m.ProductCode == DataCode);
        FileStream fs = new FileStream(path,FileMode.Create);
        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
        DataContractSerializer serializer =  new DataContractSerializer(typeof(Product));
        serializer.WriteObject(writer, prod);
    }

So, do I need to do something with Rules in order to ensure that the entire structure gets written, or does Include() know to tunnel deep into the structure and load each element?

Community
  • 1
  • 1
J.D. Ray
  • 697
  • 1
  • 8
  • 22

1 Answers1

1

You would need to explicitly include the lower levels as well.

Product prod = context.Products
    .Include(p => p.Rules.Select(r => r.PriceConditions.Select(p => p.Prices)));
Community
  • 1
  • 1
jjj
  • 4,822
  • 1
  • 16
  • 39
  • What if you don't know how many levels down the structure goes? – J.D. Ray Apr 30 '15 at 21:14
  • 1
    I tried looking around, but it seems like you'd have to explicitly `Include()` each property at some point. For example, see http://stackoverflow.com/questions/14512285/entity-framework-is-there-a-way-to-automatically-eager-load-child-entities-wit. Maybe you could ask another question about eager loading all children at specific points. – jjj May 01 '15 at 06:44