EF代码首先:检索基类型查询所有派生类型表 [英] EF Code First: Retrieving a base type queries all derived type tables

查看:132
本文介绍了EF代码首先:检索基类型查询所有派生类型表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题与EF 4.1代码第一,即使我已经配置一个实体为其继承的属性生成列,它仍然加入到继承类型的表。



这是我的课程:

  public class Human 
{
public int Id {get;组; }
public string Name {get;组; }
}

public class SuperHuman:Human
{
public int Id {get;组; }
public string Powers {get;组; }
}


public class MarvelDbContext:DbContext
{
public DbSet&Human。人类组; }
public DbSet< SuperHuman>超人组;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< SuperHuman>()。Map(m => m.MapInheritedProperties());
}
}

这是结果查询:

  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]。[名称] AS [C2],
CASE WHEN([UnionAll1]。[C2] = 1)THEN'0X'ELSE'0X0X '结束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],
演员(1作为位)AS [C2]
FROM [dbo]。[Humen] AS [Extent1]
UNION ALL
SELECT
[ Extent2]。[Id] AS [Id],
[Extent2]。[Name] AS [Name],
[Extent2]。[Powers] AS [Powers],
cast作为位)AS [C1]
FROM [dbo]。[SuperHumans] AS [Extent2])AS [UnionAll1]
)AS [Limit1]

我只想它查询人类表。

解决方案

这就是EF的行为。如果您查询 Human 设置它始终会遍历所有派生表,因为 SuperHuman 仍然是人类,因为 SuperHuman 的实例是对人类进行查询的有效结果。


I'm having an odd issue with EF 4.1 Code First where even though I have configured an entity to generate columns for its inherited properties, it still joins to the inherited type's table.

Here are my classes:

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

Here is the resulting query:

    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]

I only want it to query the Humans table.

解决方案

That is how EF behaves. If you query Human set it always goes over all derived tables as well because SuperHuman is still Human and because of that instances of SuperHuman are valid results of query for humans.

这篇关于EF代码首先:检索基类型查询所有派生类型表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆