我们正在研究Entity Framework中的延迟加载和紧急加载之间的区别。 我们正在寻找该实体创建的SQL查询。 我们有这样的省模型:
public class Province
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(150)]
public string Name { get; set; }
public int CountryId { get; set; }
public virtual Country Country { get; set; }
public virtual IList<City> City { get; set; }
}
如果我们使用急切加载,并且我们希望国家/地区属性和城市属性充满数据,那么实体将创建并像这样进行SQL查询(我们使用 .include(“ PropertyName or Expression “)加载国家和城市):
SELECT
[Project1].[Id] AS [Id],
[Project1].[Name] AS [Name],
[Project1].[CountryId] AS [CountryId],
[Project1].[Id1] AS [Id1],
[Project1].[Name1] AS [Name1],
[Project1].[C1] AS [C1],
[Project1].[Id2] AS [Id2],
[Project1].[Name2] AS [Name2],
[Project1].[ProvinceId] AS [ProvinceId]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[CountryId] AS [CountryId],
[Extent2].[Id] AS [Id1],
[Extent2].[Name] AS [Name1],
[Extent3].[Id] AS [Id2],
[Extent3].[Name] AS [Name2],
[Extent3].[ProvinceId] AS [ProvinceId],
CASE WHEN ([Extent3].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[Province] AS [Extent1]
INNER JOIN [dbo].[Country] AS [Extent2] ON [Extent1].[CountryId] = [Extent2].[Id]
LEFT OUTER JOIN [dbo].[City] AS [Extent3] ON [Extent1].[Id] = [Extent3].[ProvinceId]
) AS [Project1]
ORDER BY [Project1].[Id] ASC, [Project1].[Id1] ASC, [Project1].[C1] ASC
,当我们使用延迟加载时,它会生成SQL查询,如下所示:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[CountryId] AS [CountryId]
FROM [dbo].[Province] AS [Extent1]
,我们仍然可以访问该省的国家和城市。 我的问题是如何在不使用SQL Query的情况下延迟加载来填充国家和城市属性?(在两种情况下,我们都使用Visual Studio和SQL Profiler来获取SQL Query)