DeferredLoadingEnabled - 它是如何工作的?
我有几个表,包括多对多关系中的“组合”。我现在想做插入操作。
我听说过 linq2sql 中的 DeferredLoadingEnabled,我知道它与这个主题有关,但我不太知道它是如何工作的?
例如,
[ClientTable]: ClientId, ClientName, Phone, Age
[CityTable]: CityID, CityName, Country
[ClientCityTable]: ClientCityID, ClientID, CityID
有人可以在这个简单的示例中展示它或提供一些不错的链接吗?
I've several tables including the 'combining' in many-to-many relationship. I want to make now Insert operation.
I heard about DeferredLoadingEnabled in linq2sql and I know it's connected with this topic, but I don't quite know how it works?
For example
[ClientTable]: ClientId, ClientName, Phone, Age
[CityTable]: CityID, CityName, Country
[ClientCityTable]: ClientCityID, ClientID, CityID
Could someone show it at this simple example or provide some nice link?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
和插入完全没有关系。
延迟加载意味着 Linq-2-sql 仅在您实际使用链接表时才会访问数据库。
在您的示例中,如果您加载 [ClientCity],则在您实际使用它之前,城市名称将不会被加载。
因此,如果您这样做:
CityTable 实体将不会被加载,直到您执行以下操作:
仅在那时,CityTable 才会从数据库加载。
如果您只是偶尔需要该城市,这可能会很棒,但是一旦您循环到所有 CityTables,它可能会导致 N+1 查询
。周围有很多链接。例如: http://msdn.microsoft.com/en-us/library/bb399393 .aspx
或 http://www .west-wind.com/weblog/posts/2009/Oct/12/LINQ-to-SQL-Lazy-Loading-and-Prefetching
It has absolutely nothing to do with inserting.
Deffered loading means that Linq-2-sql will only go the database as soon as you actually use the linked tables.
In your example, if you load a [ClientCity] the Cityname will not be loaded untill you actually use it.
So if you do:
The CityTable entity will not be loaded until you do something like:
Only at that time, the CityTable is loaded from the db.
This can be great if you only occassionaly need the city but it can lead to N+1 queries as soon as you loop to all your CityTables
There are plenty of links around. E.g: http://msdn.microsoft.com/en-us/library/bb399393.aspx
or http://www.west-wind.com/weblog/posts/2009/Oct/12/LINQ-to-SQL-Lazy-Loading-and-Prefetching