易截截图软件、单文件、免安装、纯绿色、仅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操作全集

下列语句部分是Mssql语句,不可以在access中使用。
SQL分类:
DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)
DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
首先,简要介绍基础语句:
1、说明:创建数据库
CREATE DATABASE data ......

SQL SERVER 存储过程简单案例

一、简单实例
1.首先来一个最简单的存储过程吧
CREATE PROCEDURE dbo.testProcedure_AX
AS
select userID from USERS order by userid desc
注:dbo.testProcedure_AX是你创建的存储过程名,可以改为:AXzhz等,别跟关键字冲突就行了,AS下面就是一条SQL语句.
2.如何在ASP.NET中调用这个存储过程?
     &n ......

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 Server]关于对时间戳timestamp理解

一直对时间戳的概念模糊,并且网上也有很多朋友也都误认为:是一个时间字段,每次增加数据时,填入当前的时间值。导致也误导了很多朋友。
 
这次看了很多资料,纠正一下这个错误,自己也搞清楚:数据库中自动生成的唯一二进制数字,与时间和日期无关的, 通常用作给表行加版本戳的机制。存储大小为 8 个字节。
&nbs ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号