gpt4 book ai didi

asp.net - Entity Framework 4与ctp5和脏生成的sql

转载 作者:行者123 更新时间:2023-12-02 13:58:12 24 4
gpt4 key购买 nike

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

最佳答案

很好的问题。我猜测这是一个错误,因为我的(edmx 生成的) Entity Framework 上下文不会像您的那样生成投影。我会将其报告为错误。

关于asp.net - Entity Framework 4与ctp5和脏生成的sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4892676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com