如何将子类转换为父类以便在NHibernate中保存?

leo*_*ora 0 c# nhibernate inheritance

我有以下课程:

public class Parent
{
     public string Field1;
     public string Field2; 
}

public class Child : Parent
{
    public string Field3;
}
Run Code Online (Sandbox Code Playgroud)

我现在有一个Child对象列表,然后我需要传递给一个寻找Parent类的函数.

将Child类转换回Parent类的最佳方法是什么(此时我不关心field3)?

我正在使用NHibernate,当我传入子类并尝试保存它时,我收到此错误:

没有持久性:ChildClass

即使'Parent'类有持久性.

SLa*_*aks 9

子类实例是父类的实例.
你不需要做任何特别的事情.

  • 包含关于协方差的说明是有用的,因为OP正在讨论儿童名单,而不仅仅是一个孩子. (4认同)