2011-02-03 61 views
3

我有ef4 ctp5中奇怪的生成SQL的問題。實體框架4與ctp5和髒生成sql

我有簡單的模型映射:

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

和我的LINQ查詢:

_rovastampDbContext.PostAuctions.Where(x => x.PostAuctionCategoryId == auctionId).OrderBy(x => x.Id).ToList(); 

EF4探查顯示我

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 

我的問題是一個非常簡單的:爲什麼ef4生成那個複雜的查詢,當正確的是

SELECT ... 
FROM TABLE 
WHERE CategoryId = 1 
ORDER BY Id ASC 

謝謝你的建議:)

馬丁

編輯:如果我讓EF創建數據庫自動的,查詢的問題仍然存在......

+0

看起來並不複雜,首先它會在那裏執行ORDER BY – 2011-02-03 22:54:37

+0

@K Ivanov:這比需要更復雜。它通過>順序選擇>投影>選擇。 「select into projection」不是必需的,可能會導致查詢運行速度變慢。 – StriplingWarrior 2011-02-03 22:59:23

回答

0

非常好的問題。我猜這是一個錯誤,因爲我的(edmx生成的)實體框架上下文不會按照你的方式產生投影。我會將其報告爲一個錯誤。

0

是的,當然,看起來像Beta亂碼的代碼給我。我只是用LINQ-SQL也試過,沒有什麼相似之處。一定要報告!