C# Marten:在构造函数中调用AddSubclass Hierarchy()不会在查询中产生任何结果<;T>;

C# Marten:在构造函数中调用AddSubclass Hierarchy()不会在查询中产生任何结果<;T>;,c#,marten,C#,Marten,当我阅读Maven时,我认为在泛型参数为基类的地方调用addSubclass Hierarchy()会自动映射所有子类型。文件内容如下: // Alternatively, you can use the following: // _.Schema.For<ISmurf>().AddSubClassHierarchy(); // this, however, will use the assembly // of type ISmurf to get all its' subcla

当我阅读Maven时,我认为在泛型参数为基类的地方调用addSubclass Hierarchy()会自动映射所有子类型。文件内容如下:

// Alternatively, you can use the following:
// _.Schema.For<ISmurf>().AddSubClassHierarchy();
// this, however, will use the assembly
// of type ISmurf to get all its' subclasses/implementations. 
// In projects with many types, this approach will be undvisable.
然后当我尝试时:

        using (var session = store.OpenSession())
        {
            list = session.Query<Actor>().Where(a => a.Username == "asdasd").ToList();
        }
但我当然意识到,将所有对象放在一个表中是一个很容易解决的问题,但我想我读了一些其他的注意事项,我不确定我是否正确解释了:

对于类型层次结构,有几点需要注意:

  • 抽象或接口的文档类型将自动设置为层次结构
  • 如果希望使用具体类型作为层次结构的基类,则需要通过添加如上所示的子类来显式配置该类型
  • 此时,您只能在顶部的基本类型中指定“可搜索”字段
  • 子类文档类型必须可转换为顶级类型。作为 当然,Marten目前不支持“结构类型”,但可能在 未来
  • 在内部,子类类型文档也作为父类型存储在标识映射机制中
  • 为了增加这一功能,我需要花很多很多的时间在办公桌上敲打我的头
最糟糕的部分似乎是“在这一点上,您只能在顶部指定“可搜索”字段,基本类型”,但我对用户名进行了
查询,该用户名在基本类型
CoreObject
中不存在(它存在于
Actor
中)。所以我不确定这是什么意思

更新
Marten对“可搜索”的定义是“可索引的”?因为我再也不能在子类中存在的属性上创建索引了,所以看起来。

所以,我想我有了答案:

添加以下行将假定所有子类都保存在顶级表中,因此所有
Actor
Customer
都将保存在
mt\u doc\u coreobject
表中,而不是它们自己的特定表中

因此,如果您添加下面的行,Marten将假定情况是这样的,但是如果OBJET已经保存在它们自己的表中(在添加下面的行之前),则不会找到任何结果

因此,这一行:

so.Schema.For<CoreObject>().AddSubClassHierarchy();
so.Schema.For().addSubclass层次结构();

然后将要求您重新插入对象,然后它们出现在
mt_doc_coreobject

中,Jeremy Miller@Marten写道:“通过抽象的接口通过具体类型的东西并不重要。我怀疑是自动类型发现的问题。首先尝试手动定义层次结构,看看这是否真的是问题所在,然后我们回到类型扫描无法找到您要查找的内容的原因”
        using (var session = store.LightweightSession())
        {
            List<Customer> list = session.Query<Customer>().ToList();
            return list;
        }
public MartenDbHandler()
{
    StoreOptions so = new StoreOptions();
    // here it is: CoreObject-->Actor-->Customer
    so.Schema.For<CoreObject>().AddSubClassHierarchy(typeof(Actor), typeof(Customer));
    so.Connection("host=localhost;database=marten;password=root;username=postgres");
    so.AutoCreateSchemaObjects = AutoCreate.CreateOrUpdate;
    SetTableMeta(so);
    store = new DocumentStore(so);
}
    List<Actor> list3 = martenDbHandler.Select<Actor>(c => c.Username == "YS3M");
    Console.WriteLine($"SELECT Actor {list3.Count}"); // = 1
so.Schema.For<CoreObject>().AddSubClassHierarchy();