Inheritance 如何在Automapper 9.0中将基类映射到派生类

Inheritance 如何在Automapper 9.0中将基类映射到派生类,inheritance,automapper,derived-class,base-class,Inheritance,Automapper,Derived Class,Base Class,这个问题只出现在Automapper 9中,在此之前,一切都对我有效。 这是我的数据结构Ж public class TestEntity { public BaseEntity Base { get; set; } } public class BaseEntity { public string Text1 { get; set; } public string Text2 { get; set; } } public class DerivedEntity :

这个问题只出现在Automapper 9中,在此之前,一切都对我有效。 这是我的数据结构Ж

public class TestEntity
{
    public BaseEntity Base { get; set; }
}

public class BaseEntity
{
    public string Text1 { get; set; }
    public string Text2 { get; set; }
}

public class DerivedEntity : BaseEntity
{
    public int Num1 { get; set; }
    public int Num2 { get; set; }
}

public class TestDto
{
    public BaseDto Base { get; set; }

}

public class BaseDto
{
    public string Text1 { get; set; }
    public string Text2 { get; set; }
}

public class DerivedDto : BaseDto
{
    public int Num1 { get; set; }
    public int Num2 { get; set; }
}
此代码不会返回派生类:

var test2 = new TestEntity { Base = new DerivedEntity() };
var testDto2 = mapper.Map<TestDto>(test2);
var test2=newtestentity{Base=new DerivedEntity()};
var testDto2=mapper.Map(test2);
重做会有帮助。制作一个我们可以执行并看到失败的程序。重做会有所帮助。制定一个我们可以执行并看到失败的计划。