本文介绍了在RavenDB中指定集合名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以可以说我有3个对象:水果,苹果和橘子.水果是苹果和橘子的抽象基类.当我使用session.Store(myApple)时,它将其放入Apples集合中. myOrange存储在Oranges集合中.说得通.

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.

我可以告诉Raven我想要一个可以装苹果或橙子的水果系列吗? Mongodb允许这样做,因为它使我可以明确指定集合名称. RavenDB集合文档说:

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:

我希望它类似于:session.Store< Fruit>(myApple)或session.Store("Fruits",myApple)

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

有什么想法吗?谢谢.

推荐答案

锥子,您可以使用:

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);

这将处理所有问题.

这篇关于在RavenDB中指定集合名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 16:38