【问题标题】:Entity framework 4 with ctp5 and dirty generated sql带有 ctp5 和脏生成 sql 的实体框架 4
【发布时间】:2016-08-05 04:40:29
【问题描述】:

我在 ef4 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 自动创建数据库,查询问题仍然存在......

【问题讨论】:

  • 看起来一点也不复杂,首先是 WHERE,然后是 ORDER BY
  • @K Ivanov:这比必要的复杂。它在哪里>排序>选择投影>选择。 “select into projection”不是必需的,可能会导致查询运行速度变慢。
  • 出于好奇,如果您从类中删除 IsAuctionPhotoExitst 属性,它的行为是否相同?
  • @StriplingWarrior 去掉 NotMaped 属性后,查询还是一样

标签: asp.net sql entity-framework-4


【解决方案1】:

很好的问题。我猜这是一个错误,因为我的(edmx 生成的)实体框架上下文不会像你的那样产生投影。我会将其报告为错误。

【讨论】:

  • 嗯,很有趣。顺便说一句,我将 ctp5 与现有数据库一起使用,并将我的域模型映射到现有表。
【解决方案2】:

是的,对我来说,这看起来像是 Beta 乱码代码生成器。我也用 LINQ-SQL 尝试过,没有类似的。一定要举报!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多