Entity framework EF代码优先:检索基类型查询所有派生类型表
EF4.1代码首先有一个奇怪的问题,尽管我配置了一个实体为其继承属性生成列,但它仍然连接到继承类型的表 以下是我的课程:Entity framework EF代码优先:检索基类型查询所有派生类型表,entity-framework,entity-framework-4.1,ef-code-first,Entity Framework,Entity Framework 4.1,Ef Code First,EF4.1代码首先有一个奇怪的问题,尽管我配置了一个实体为其继承属性生成列,但它仍然连接到继承类型的表 以下是我的课程: public class Human { public int Id { get; set; } public string Name { get; set; } } public class SuperHuman : Human { public int Id { get; set; } public string Powers { get
public class Human
{
public int Id { get; set; }
public string Name { get; set; }
}
public class SuperHuman : Human
{
public int Id { get; set; }
public string Powers { get; set; }
}
public class MarvelDbContext : DbContext
{
public DbSet<Human> Humans { get; set; }
public DbSet<SuperHuman> SuperHumans { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SuperHuman>().Map(m => m.MapInheritedProperties());
}
}
我只想让它查询Humans表。这就是EF的行为方式。如果您查询
Human
set,它也会遍历所有派生表,因为SuperHuman
仍然是Human
,而且SuperHuman
的实例是对Human的有效查询结果。您可以包括LINQ
查询和完整的OnModelCreating
方法体吗。您是否使用每种类型的表格
?
SELECT
[Limit1].[C3] AS [C1],
[Limit1].[C1] AS [C2],
[Limit1].[C2] AS [C3],
[Limit1].[C4] AS [C4]
FROM ( SELECT TOP (1)
[UnionAll1].[Id] AS [C1],
[UnionAll1].[Name] AS [C2],
CASE WHEN ([UnionAll1].[C2] = 1) THEN '0X' ELSE '0X0X' END AS [C3],
CASE WHEN ([UnionAll1].[C2] = 1) THEN CAST(NULL AS varchar(1)) ELSE [UnionAll1].[C1] END AS [C4]
FROM (SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
CAST(NULL AS varchar(1)) AS [C1],
cast(1 as bit) AS [C2]
FROM [dbo].[Humen] AS [Extent1]
UNION ALL
SELECT
[Extent2].[Id] AS [Id],
[Extent2].[Name] AS [Name],
[Extent2].[Powers] AS [Powers],
cast(0 as bit) AS [C1]
FROM [dbo].[SuperHumans] AS [Extent2]) AS [UnionAll1]
) AS [Limit1]