1

Given that I have the following structure (unnecessary details stripped out)

public class Product {
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Manufacturer Manufacturer { get; set; }
}

public class Manufacturer {
    public Guid Id { get; set; }
    public string Name { get; set; }
}

If I have a lot of these kind of products stored in raven and I want to index them by manufacturer id (or maybe some other things as well) I'd make an index such as this (of course in real life this index also contains some other information as well...)

public class ProductManufacturerIndex : AbstractIndexCreationTask<Product> {
    public ProductManufacturerIndex() {
        Map = products => from product in products
                          select new {
                              Manufacturer_Id = product.Manufacturer.Id,
                          };
    }
}

My question here is, why do I need to name my field Manufacturer_Id? If I do not name it Manufacturer_Id I get exceptions when attempting to query my index since the manufacturer id column is not indexed.

Basically, why can't I do this? (Which would be my first guess)

public class ProductManufacturerIndex : AbstractIndexCreationTask<Product> {
    public ProductManufacturerIndex() {
        Map = products => from product in products
                          select new {
                              product.Manufacturer.Id,
                          };
    }
}
wasatz
  • 4,158
  • 2
  • 23
  • 30

1 Answers1

1

There is a naming convention that RavenDB uses. If you aren't naming your fields properly, it doesn't know how to map things.

In this case, the second index you use has a property of Id, but RavenDB has no way of knowing that you mapped the Manufacturer's id, and not the root id.

That is why we have this convention. You can change it if you really want to, but it is generally not recommended.

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
  • Well...that makes sense when you put it like that. I'm guessing that this will work "downwards" in the structure as well? So for example if my manufacturers had a single Location object as well I should call that Manufacturer_Location_Id and so on? – wasatz Oct 13 '14 at 11:20