积极答复者
EF里这个方法是什么意思?

问题
答案
-
在ef裡面有所謂的Lazy Loading 和 Eager Loading。
用下面的例子介紹差別:
public class Blog { public int BlogId { get; set; } public string Title { get; set; } public ICollection<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public Blog Blog { get; set; } public ICollection<Comment> Comments { get; set; } } public class Comment { public int CommentId { get; set; } public string CommentText { get; set; } public Post Post { get; set; } }
這三個Class分別會在DB裡面建立對應的Table,假設今天我撈出了一個Post,而從上面可以看出一個Post有多個Comment。從C#的角度來說,我如果要看相關的Comment,我會很直覺的直接作出以下:
var comment = dbcontext.Post.First().Comments
但是上面這一段程式碼會報錯,原因是預設EF是不會Load Comments出來(Lazy Load 是預設同時 因為使用ICollection),如果你要叫他Load出來,需要告知他,使用Include的方式
var comment = dbcontext.Post.Include("Comments").First().Comments
回到你的問題,說實在的爲什麽Include為空字串我不是很瞭解,是否有寫錯?不過Include的作用如我上面描述那樣。
最後如果想知道更詳細,可以看 http://msdn.microsoft.com/en-us/data/jj574232- 已建议为答案 ThankfulHeart 2013年8月18日 3:21
- 已标记为答案 Fred BaoModerator 2013年10月22日 3:13
-
Include() 方法中应该是导航属性,就是强制加载用的。EF 默认是启用延迟加载的,这个时候你若是在程序中使用到了 aspnet_Users的任何导航属性,那系统就会报错。
所以在写代码时若是想要使用 EF 导航属性所带来的便利,那就得设置一下。
Please mark this as answer if it helps with this issue!
- 已标记为答案 Fred BaoModerator 2013年10月22日 3:13
全部回复
-
在ef裡面有所謂的Lazy Loading 和 Eager Loading。
用下面的例子介紹差別:
public class Blog { public int BlogId { get; set; } public string Title { get; set; } public ICollection<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public Blog Blog { get; set; } public ICollection<Comment> Comments { get; set; } } public class Comment { public int CommentId { get; set; } public string CommentText { get; set; } public Post Post { get; set; } }
這三個Class分別會在DB裡面建立對應的Table,假設今天我撈出了一個Post,而從上面可以看出一個Post有多個Comment。從C#的角度來說,我如果要看相關的Comment,我會很直覺的直接作出以下:
var comment = dbcontext.Post.First().Comments
但是上面這一段程式碼會報錯,原因是預設EF是不會Load Comments出來(Lazy Load 是預設同時 因為使用ICollection),如果你要叫他Load出來,需要告知他,使用Include的方式
var comment = dbcontext.Post.Include("Comments").First().Comments
回到你的問題,說實在的爲什麽Include為空字串我不是很瞭解,是否有寫錯?不過Include的作用如我上面描述那樣。
最後如果想知道更詳細,可以看 http://msdn.microsoft.com/en-us/data/jj574232- 已建议为答案 ThankfulHeart 2013年8月18日 3:21
- 已标记为答案 Fred BaoModerator 2013年10月22日 3:13
-
Include() 方法中应该是导航属性,就是强制加载用的。EF 默认是启用延迟加载的,这个时候你若是在程序中使用到了 aspnet_Users的任何导航属性,那系统就会报错。
所以在写代码时若是想要使用 EF 导航属性所带来的便利,那就得设置一下。
Please mark this as answer if it helps with this issue!
- 已标记为答案 Fred BaoModerator 2013年10月22日 3:13