【发布时间】:2021-03-06 04:34:33
【问题描述】:
我想使用FromSqlRaw 查询与视图模型和它们之间的继承。
我创建了两个示例视图模型:
public class SummaryVM
{
public int Quantity { get; set; }
public int Value { get; set; }
}
public class DistrictVM : SummaryVM
{
public string DistrictName { get; set; }
}
我的DbContext 有这个内容:
public DbSet<SummaryVM> SummaryVMs { get; set; }
public DbSet<DistrictVM> DistrictVMs { get; set; }
...
modelBuilder.Entity<SummaryVM>().HasNoKey();
我的查询:
return await _context.DistritVMs.FromSqlRaw(query).ToListAsync();
但我得到一个错误:
SqlException:列名“鉴别器”无效。列名“区域名称”无效
我知道这是由于 TPH 模式,如果我不使用继承,我可以解决这个问题,但是如何在没有 TPH 的情况下使用这两个具有继承的表?
解决方案 我必须创建一个未注册为实体的基类:
public abstract BaseClass
{
public int Quantity { get; set; }
public int Value { get; set; }
}
public class SummaryVM : BaseClass
{
}
public class DistrictVM : BaseClass
{
public string DistrictName { get; set; }
}
【问题讨论】:
标签: entity-framework inheritance rawsql