[转自雨痕]LINQ to SQL - Delay Loaded
原文: http://www.rainsts.net/article.asp?id=550
对 ORM 而言,延迟加载(也叫 Lazy Loading)是必须的,毕竟当我们只是获取用户基本信息时,总不能动不动就返回大段的用户个人简介不是。
在 VS 2008 O/R Designer 中,很容易找到 Delay Loaded 的设置。
只是当我在自动生成的代码中找寻这个设置时,着实迷糊了半天。基于惯性思维,我查看了几乎所有的特性(Attribute)声明,却一无所获,最后才发觉这个家伙竟然是这个样子。
Delay Loaded = False
[Table(Name="dbo.[User]")]
public partial class User : INotifyPropertyChanging, INotifyPropertyChanged
{
private string _Job;
}
public partial class User : INotifyPropertyChanging, INotifyPropertyChanged
{
private string _Job;
}
Delay Loaded = True
[Table(Name="dbo.[User]")]
public partial class User : INotifyPropertyChanging, INotifyPropertyChanged
{
private System.Data.Linq.Link<string> _Job;
}
public partial class User : INotifyPropertyChanging, INotifyPropertyChanged
{
private System.Data.Linq.Link<string> _Job;
}
习惯害人~~~~ 接下来我们测试一下延迟加载的作用。
using (DataClasses1DataContext context = new DataClasses1DataContext())
{
context.Log = Console.Out;
var user = (from u in context.Users where u.Name == "user1" select u).FirstOrDefault();
Console.WriteLine(user.Name);
Console.WriteLine(user.Job);
}
{
context.Log = Console.Out;
var user = (from u in context.Users where u.Name == "user1" select u).FirstOrDefault();
Console.WriteLine(user.Name);
Console.WriteLine(user.Job);
}
Delay Loaded = False 输出
SELECT TOP 1 [t0].[Id], [t0].[Name], [t0].[Age], [t0].[Job]
FROM [dbo].[User] AS [t0]
WHERE [t0].[Name] = @p0
-- @p0: Input String (Size = 5; Prec = 0; Scale = 0) [user1]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1
user1
job
FROM [dbo].[User] AS [t0]
WHERE [t0].[Name] = @p0
-- @p0: Input String (Size = 5; Prec = 0; Scale = 0) [user1]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1
user1
job
Delay Loaded = True 输出
SELECT TOP 1 [t0].[Id], [t0].[Name], [t0].[Age]
FROM [dbo].[User] AS [t0]
WHERE [t0].[Name] = @p0
-- @p0: Input String (Size = 5; Prec = 0; Scale = 0) [user1]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1
user1
SELECT [t0].[Job]
FROM [dbo].[User] AS [t0]
WHERE [t0].[Name] = @p0
-- @p0: Input String (Size = 5; Prec = 0; Scale = 0) [user1]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1
job
FROM [dbo].[User] AS [t0]
WHERE [t0].[Name] = @p0
-- @p0: Input String (Size = 5; Prec = 0; Scale = 0) [user1]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1
user1
SELECT [t0].[Job]
FROM [dbo].[User] AS [t0]
WHERE [t0].[Name] = @p0
-- @p0: Input String (Size = 5; Prec = 0; Scale = 0) [user1]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1
job
当设置延迟加载后,查询 User 时并没有返回 Job 字段值,只有当访问该属性时才进行了第二次 SQL 操作,很显然这就是我们所需要的。延迟加载虽然很有用,但并不表示我们应该将 n 多的 Column 设置成 Delay Loaded = true,或许你要做的是创建另外一个 One-to-One / Base-to-Detail。
注意!当 "context.DeferredLoadingEnabled = false" 或 "context.ObjectTrackingEnabled = false" 时,都会导致 Delay Loaded 失效,不再进行延迟字段载入操作。
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。