我正在使用 Table Per Hierarchy 数据库继承,其中所有派生类型的列都在一个表中。每个派生表都使用包含派生类名称的字符串鉴别器字段进行标识:
---------------------
| tanimal |
---------------------
| animalid |
| discriminator |
| furcolour |
| feathercolour |
---------------------
public abstract class Animal
{
public int AnimalId { get; set; }
public string Discriminator { get { return GetType().Name; } }
}
public class Bird : Animal
{
public string FeatherColour { get; set; }
}
public class Dog : Animal
{
public string FurColour { get; set; }
}
正如预期的那样,当通过 Dapper 的查询方法检索它时,我收到Instances of abstract classes cannot be created
. 我希望这将返回一个 Animal 列表,它们的值是各自的派生类型。
var animals = Connection.Query<Animal>("SELECT * FROM tanimal")
我尝试添加对此的支持没有成功。如果传入的类型是抽象类,则在调用 SqlMapper.cs::GetTypeDeserializer() 之前,我将类型替换为以下方法中返回的类型:
static Type GetDerivedType(Type abstractType, IDataReader reader)
{
var discriminator = abstractType.GetProperty("Discriminator");
if (discriminator == null)
throw new InvalidOperationException("Cannot create instance of abstract class " + abstractType.FullName + ". To allow dapper to map to a derived type, add a Discriminator field that stores the name of the derived type");
return Type.GetType((string)reader["Discriminator"]);
}
但是,此时阅读器似乎尚未打开,因此失败并显示Invalid attempt to read when no data is present
.
这是正确的方法吗?是否有任何努力在其他地方支持这一点?