30

So lets say I have 3 objects Fruit, Apple and Orange. Fruit is the abstract base class for Apple and Orange. When I use session.Store(myApple), it puts it into the Apples collection. myOrange stores in the Oranges collection. Makes sense.

Can I tell Raven that I want a Fruits collection that could hold Apples or Oranges? Mongodb allows this since it lets me explicitly specify the collection name. The RavenDB collections documentation says:

The expected usage pattern is that collections are used to group documents with similar structure, although that is not required. From the database standpoint, a collection is just a group of documents that share the same entity name.

I'd expect it to be something like: session.Store<Fruit>(myApple), or session.Store("Fruits", myApple)

Any ideas? Thanks.

awl
  • 1,540
  • 1
  • 15
  • 18
  • These seem similar to your issue. I'd try this but I'm unable at the moment. http://groups.google.com/group/ravendb/browse_thread/thread/eb86c2ffd29ed5e/11644a7eaddaf123?lnk=gst&q=Inheritance#11644a7eaddaf123 http://mikehadlow.blogspot.com/2010/10/ravendb-playing-with-inheritance-and.html – Derek Beattie Aug 07 '11 at 03:35

1 Answers1

39

Awl, You can do it using:

session.Store(apple);
session.Advanced.GetMetadataFor(apple)[Constants.RavenEntityName] = "Fruits";

That is the long way to do so. A much better way would be to add this logic globally, it looks something like this:

store.Conventions.FindTypeTagName = 
   type => type.IsSubclassOf(typeof(Fruit)) ? 
       DocumentConvention.DefaultTypeTagName(typeof(Fruit)) : 
       DocumentConvention.DefaultTypeTagName(type);

That will handle this for everything.

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
  • For .NET Core you need to use `type.GetTypeInfo().IsSubclassOf(typeof(Fruit))` (from `System.Reflection`) or `typeof(Fruit).IsAssignableFrom(type)` ([link](http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx)). Keep in mind some gotchas for the `IsAssignableFrom` ([link](https://stackoverflow.com/a/2742288/968003)) – Alex Klaus Jun 09 '17 at 05:46
  • to better understand this answer, please look here https://ravendb.net/docs/article-page/3.5/all/client-api/session/configuration/how-to-customize-collection-assignment-for-entities – pomarc Oct 31 '17 at 16:30