I have a problem with strange generated SQL in ef4 ctp5.
I have simple model with mapping :
[Table("post_auction")]
public class PostAuction
{
[Key,Column(Name="Id"),DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGenerationOption.Identity)]
public int Id { get; set; }
[Column(Name = "Number")]
public int Number { get; set; }
[Column(Name = "Label")]
public string Label { get; set; }
[Column(Name = "Description")]
public string Description { get; set; }
[Column(Name = "CategoryId")]
public int PostAuctionCategoryId { get; set; }
[Column(Name = "PriceCZK")]
public int PriceCZK { get; set; }
[NotMapped]
public bool IsAuctionPhotoExitst
{
get
{
if (File.Exists(HttpContext.Current.Server.MapPath("~/Public/Images/Posts/Thumbs/small_" + this.Number + ".jpg")))
return true;
return false;
}
}
}
and my linq query is :
_rovastampDbContext.PostAuctions.Where(x => x.PostAuctionCategoryId == auctionId).OrderBy(x => x.Id).ToList();
Ef4 profiler shows me
SELECT
[Project1].[Id] AS [Id],
[Project1].[Number] AS [Number],
[Project1].[Label] AS [Label],
[Project1].[Description] AS [Description],
[Project1].[CategoryId] AS [CategoryId],
[Project1].[PriceCZK] AS [PriceCZK]
FROM
(SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Number] AS [Number],
[Extent1].[Label] AS [Label],
[Extent1].[Description] AS [Description],
[Extent1].[CategoryId] AS [CategoryId],
[Extent1].[PriceCZK] AS [PriceCZK]
FROM
[dbo].[post_auction] AS [Extent1]
WHERE
[Extent1].[CategoryId] = 1 /* @p__linq__0 */) AS [Project1]
ORDER BY
[Project1].[Id] ASC
My question is a pretty simple : Why ef4 generate that complicated query, when right one is
SELECT ...
FROM TABLE
WHERE CategoryId = 1
ORDER BY Id ASC
Thanks for your advice :)
Martin
EDIT : If I let EF to create db automatic, the problem with query persists...
Excellent question. I'm guessing that it's a bug, since my (edmx-generated) Entity Framework context doesn't produce a projection the way yours does. I would report it as a bug.