Reputation: 204239
I'm using Entity Framework 4.1's code first approach to map a class hierarchy to a series of tables.
I have the following classes defined:
[Table("FooBases")]
public class FooBase
{
public int Id { get; set; }
public string Name { get; set; }
}
[Table("Foo1s")]
public class Foo1 : FooBase
{
public string Details { get; set; }
}
[Table("Foo2s")]
public class Foo2 : FooBase
{
public string Description { get; set; }
}
So this will create three tables, where the common properties (Id and Name) are stored in a FooBase
table.
In one particular case I am only interested in a list of names of any kind of FooBase
object. I don't care whether it's a Foo1
or a Foo2
. I spin up a new data context and run a query like this:
var names = ctx.Set<FooBase>().Select(f => f.Name);
Now, I don't need the overhead of querying the Foo1s
and Foo2s
tables. I expect the query to just be a simple SELECT Name FROM FooBases
. Instead, I get this:
SELECT
[Extent1].[Name] AS [Name]
FROM [dbo].[FooBases] AS [Extent1]
LEFT OUTER JOIN (SELECT
[Extent2].[Id] AS [Id]
FROM [dbo].[Foo1s] AS [Extent2]
UNION ALL
SELECT
[Extent3].[Id] AS [Id]
FROM [dbo].[Foo2s] AS [Extent3]) AS [UnionAll1] ON [Extent1].[Id] = [UnionAll1].[Id]
Is there a way to tell Entity Framework that I don't care about the other tables; that I just want data from the top level of the class hierarchy so there's no need to query anything other than the top level table?
Upvotes: 2
Views: 297
Reputation: 364369
This is well known issue with Table-per-type inheritance mapping. It should be addressed in upcoming EFv4.2 which should improve the query generation for TPT inheritance. At the moment there is no way to improve it.
Upvotes: 3