易截截图软件、单文件、免安装、纯绿色、仅160KB

Linq to SQL DataContext Lifetime Management


Linq to SQL uses a DataContext to manage it's access to the database as well as tracking changes made to entities retrieved from the database. Linq to SQL has a persistent approach to managing its 'connection' to the database via this data context and it basically assumes that you use a single DataContext to make all of your data related access. This doesn't mean that it makes persistent connections to the database, but means that the DataContext instance maintains state about active result sets, which is especially important if change tracking is on which is the default.
This is somewhat contrary to other ORM tools which tend to have a static manager to which you pass entities or queries that are then returned. In that respect most other ORMs are stateless in their data connectivity and management object where LINQ to SQL clearly takes a connected approach where everything revolves around this single DataContext instance.  DataContext holds all the change management information and it makes it very difficult to transfer that context information to another DataContext. In short it's a very different approach and requires some thinking about how you create and manage the DataContext object.
BTW, the ADO.NET Entity framework too uses a similar connected approach with its ObjectContext object which also manages state persistently and requires that you keep the object around if you want to do things like track changes.
This raises some interesting questions on how to manage the lifetime of the DataContext object. There are a lot of options of how you can deal hanging on (or not) to the DataContext. Here are a few different approaches:
Create a new Context for each atomic operation (Application level management)
Create a global DataContext and handle all operations against this single DataContext object
Create a thread specific DataContext
Create a per business object DataContext
What doesn't work
The first thing to understand is if you are coming from an


相关文档:

【摘】存储过程比较动态SQL的优点在哪里

存储过程跟动态sql相比有如下优点:
1、 存储过程允许标准组件式编程
存储过程在被创建以后可以在程序中被多次调用而不必重新编写该存储过程的SQL
语句而且数据库专业人员可随时对存储过程进行修改但对应用程序源代码毫无影响因
为应用程序源代码只包含存储过程的调用语句从而极大地提高了程序的可移植性
2 、存储过程 ......

LINQ to Entities 实现sql 关键字"In"方式总结

在LINQ to Entities中没有办法再像 LINQ to SQL 中一样使用 Contains 的方法来实现sql "in" 关键字
下面代码在 LINQ to SQL 中可行 在LINQ to Entities却无法运行: 
var s = db.Account.Select(c => c.ID);
var ret =(from t in db.Profile
 where&n ......

SQL注入法攻击 详解

随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多。但是由于程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患。用户可以提交一段数据库查询代码,根
据程序返回的结果,获得某些他想得知的数据,这就是所谓的SQL ......

数据库命名规范(适用SQL Server)

设计原则
 
符号三大范式(每一列表达一个意思,每一行代表一个实例/每一行有唯一键/表内没有其它表的非主键信息)
 
每个表应该有的3个有用字段(记录创建或更新时间/记录创建者/记录版本)
 
避免保留字
 
表应避免可为空的列


 
命名规范
 

表名如Or ......

SQL 2000 Insert返回自动编号id三种方法比较


SQL Server 2000中,insert数据的时候返回自动编号的id,有三种方法实现SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY,它们都返回插入到 IDENTITY 列中的值。
IDENT_CURRENT :返回为任何会话和任何作用域中的特定表最后生成的标识值。IDENT_CURRENT 不受作用域和会话的限制,而受限于指定的表。IDENT_CURRENT 返回为任 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号