表演:
using (var DB = new SiteEntities())
{
var test = DB.All<AssessmentItem>();
}
结果在这个 SQL 查询中,为什么要执行连接?!我根本没有声明要查询 Products 表,这导致我出现重复数据的问题。我从未见过这种行为。
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent1].[Status] AS [Status],
[Extent1].[UpdatedAt] AS [UpdatedAt],
[Extent1].[CreatedAt] AS [CreatedAt],
[Extent1].[Alias] AS [Alias],
[Extent1].[ImageFileName] AS [ImageFileName],
[Extent1].[ReplacementCost] AS [ReplacementCost],
[Extent2].[ID] AS [ID1]
FROM [dbo].[AssessmentItems] AS [Extent1]
LEFT OUTER JOIN [dbo].[Products] AS [Extent2] ON ([Extent2].[AssessmentItemID] IS NOT NULL) AND ([Extent1].[ID] = [Extent2].[AssessmentItemID])
这是我由实体框架生成的 AssessmentItem.cs:
public partial class AssessmentItem
{
public AssessmentItem()
{
this.AssessmentItemAttributeMappings = new HashSet<AssessmentItemAttributeMapping>();
this.CustomerAssessmentItems = new HashSet<CustomerAssessmentItem>();
}
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public byte Status { get; set; }
public Nullable<System.DateTime> UpdatedAt { get; set; }
public System.DateTime CreatedAt { get; set; }
public string Alias { get; set; }
public string ImageFileName { get; set; }
public Nullable<decimal> ReplacementCost { get; set; }
public virtual ICollection<AssessmentItemAttributeMapping> AssessmentItemAttributeMappings { get; set; }
public virtual ICollection<CustomerAssessmentItem> CustomerAssessmentItems { get; set; }
public virtual Product Product { get; set; }
}
更新:
using (var DB = new SiteEntities())
{
var test = DB.All<AssessmentItem>()
.Select(x => new
{
x.ID,
x.Name
});
}
产品正确的 SQL 查询,一定有某种时髦的推理在后台进行。我仍然想知道是否有人对原因有任何想法。