Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework EF Core 5和FromSqlRaw与继承_Entity Framework_Inheritance_Rawsql - Fatal编程技术网

Entity framework EF Core 5和FromSqlRaw与继承

Entity framework EF Core 5和FromSqlRaw与继承,entity-framework,inheritance,rawsql,Entity Framework,Inheritance,Rawsql,我想使用一个FromSqlRaw查询来查看模型和它们之间的继承。 我创建了两个示例视图模型: public class SummaryVM { public int Quantity { get; set; } public int Value { get; set; } } public class DistrictVM : SummaryVM { public string DistrictName { get; set; } } MyDbContext包含以

我想使用一个
FromSqlRaw
查询来查看模型和它们之间的继承。 我创建了两个示例视图模型:

public class SummaryVM 
{
    public int Quantity { get; set; }
    public int Value { get; set; }
}

public class DistrictVM : SummaryVM 
{
    public string DistrictName { get; set; }
}
My
DbContext
包含以下内容:

public DbSet<SummaryVM> SummaryVMs { get; set; }
public DbSet<DistrictVM> DistrictVMs { get; set; }
...
modelBuilder.Entity<SummaryVM>().HasNoKey();
但我有一个错误:

SqlException:列名“鉴别器”无效。无效的列名“DistrictName”

我知道这是由于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; }
}
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; }
}
解决方案 我必须创建一个未注册为实体的基类:

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; }
}
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; }
}