如何LINQ to SQL中使用SQL连接的工作原理? [英] How linq to sql works with sql connections?

查看:183
本文介绍了如何LINQ to SQL中使用SQL连接的工作原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是LINQ的数据环境中工作的最佳方法是什么?以及如何使用SQL连接(什么时候才能打开连接,它什么时候关闭连接)做链接的工作?我应该aloways使用using语句时,我的工作与数据上下文?

What is the best way of working with linq data context? And how does link work with sql connection (when does it open connection and when does it close connection)? Should I aloways use using statement when i work with data context?

抱歉的问题列表。先谢谢了。

Sorry for list of questions. Thanks in advance.

推荐答案

还有很多在这里介绍 - 我给你我的盆栽观 - 然后给你一些链接等,以帮助您进一步

There's a lot to cover here - I'll give you my potted view - and then give you some links etc to help you further.

有解决这个网站的许多其他类似的问题还有,虽然 - 所以做一个搜索,你会发现更多的

There are many other similar questions around this site as well, though - so do a search and you'll find more.

终身

  • 您不要的需要可以使用使用语句一个LINQ到SQL DataContext的,但你必须处理它时,你就大功告成了。因此,使用块是因为它的尝试......终于{}语义的最佳途径。所以,在我看来,你的应该的使用使用块。

  • You don't need to use a using statement with a Linq-To-Sql DataContext, but you do have to dispose it when you're done. Therefore the using block is the best way because of it's try ... finally {} semantics. So, in my opinion you should use a using block.

您应该的不可以保持的LINQ to SQL数据上下文,因为这发生在每次更改跟踪打开很长一段时间,对象,上下文打开 - 它损害的表现,并可能导致意外的行为。

You should not keep linq to sql data contexts open for long periods of time, because of the change tracking that occurs in every object that the context opens - it damages performances and can cause unexpected behaviour.

SQL连接,据我所知,打开时的上下文构造,收处置时。

The Sql Connection, as far as I'm aware, is opened when the context is constructed, and closed when it is disposed.

因此​​,你应该当你要选择,插入,更新或删除数据打开一个LINQ to SQL的情况下,然后将其关闭,一旦你就大功告成了。通过相同的DC插入100条记录是好的,例如;如更新或删除。不过,我可能不会选择一个几千条记录的一种类型,更新的另一几千,然后删除一些的另一个在同一个环境 - 主要是出于性能的考虑

Therefore, you should open a linq to sql context when you want to select, insert, update or delete data, and then close it once you're done. Inserting 100 records via the same DC is fine, for example; as is updating or deleting. However I probably wouldn't select a few thousand records of one type, update a few thousand of another, then delete some more of another all in one context - primarily for performance reasons.

相关数据

如果您有相关的数据 - 即表A - >表B,后来才知道,默认为的DataContext(DC)是懒加载从A.子属性来访问的时候。因此,如果在B中的相关行你打算传回你的LINQ to SQL生成的类之一的code,创建并部署用于选择特区外,你可以得到的ObjectDisposedException Ş得到抛出。

If you have related data - i.e. Table A -> Table B, then know that the default for the DataContext (DC) is to lazy-load a related row in B when accessed from the child property on A. Therefore, if you intend to pass back one of your linq to sql-generated classes outside of the code that creates and disposes the DC that is used to select, you can get ObjectDisposedExceptions getting thrown.

要避免这一点 - 您可以使用DataLoadOptions.LoadWith<>和DataLoadOptions.AssociateWith<>,以减轻这也是 - 这迫使$当选择一排p $ P取的相关行。这可以增加数据,你的LINQ to SQL从数据库中读取查询,虽然量,所以你会的没有一般的总是的申请$ P $所有相关记录都查询pfetches - 只是那些你知道会需要它们

To avoid this - you can use the DataLoadOptions.LoadWith<> and DataLoadOptions.AssociateWith<> to mitigate this also - which forces a pre-fetch of related rows when one row is selected. This can increase the amount of data that your Linq To Sql queries read from the database, though, so you would not typically always apply prefetches of all related records for all queries - only on those that you know will require them.

对不起,我不给code实例或其他任何东西 - 有很多的一个答案来覆盖 - 但这里的一些链接:

Sorry, I'm not giving code examples or anything else - there's a lot to cover in one answer - but here's some links:

<一个href="http://west-wind.com/weblog/posts/246222.aspx?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+RickStrahl+%28Rick+Strahl%27s+WebLog%29"相对=nofollow>里克施特拉尔谈论LINQ to SQL的DC寿命

<一个href="http://blogs.msdn.com/dinesh.kulkarni/archive/2008/04/27/lifetime-of-a-linq-to-sql-datacontext.aspx"相对=nofollow>微软的迪内希谈论同样的

斯科特·格思里 - 9部分的LINQ to SQL系列 - 这是一个LINQ到他的博客搜索 - 向下滚动,并通过页面,你会发现所有的不同部分,从第一部分早在2007年5月

Scott Guthrie - 9 Part Linq to SQL Series - this is a linq to a search on his blog - scroll down, and page through, and you'll find all of the different parts, starting with part 1 back in May 2007.

这篇关于如何LINQ to SQL中使用SQL连接的工作原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆