多个DataContext的类有史以来合适? [英] Are Multiple DataContext classes ever appropriate?

查看:159
本文介绍了多个DataContext的类有史以来合适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了充分利用LinqToSql在ASP.net 3.5应用程序,就必须创建的 DataContext的 (这通常是使用设计的VS 2008)。从UI的角度来看,DataContext的是你的数据库的部分的设计,你想暴露于通​​过LinqToSql,是不可或缺的设定LinqToSql最多的ORM功能。

In order to fully use LinqToSql in an ASP.net 3.5 application, it is necessary to create DataContext classes (which is usually done using the designer in VS 2008). From the UI perspective, the DataContext is a design of the sections of your database that you would like to expose to through LinqToSql and is integral in setting up the ORM features of LinqToSql.

我的问题是:我设置使用,所有表都互相连接以某种方式通过外键一个大型数据库的项目。我的第一个倾向是使一个巨大的DataContext类车型的整个数据库。这样,我在理论上可以(虽然我不知道这是否会需要在实践中)使用通过LinqToSql生成的外键连接,可以轻松地之间相关物体在我的code去,插入相关的对象,等等。

My question is: I am setting up a project that uses a large database where all tables are interconnected in some way through Foreign Keys. My first inclination is to make one huge DataContext class that models the entire database. That way I could in theory (though I don't know if this would be needed in practice) use the Foreign Key connections that are generated through LinqToSql to easily go between related objects in my code, insert related objects, etc.

不过,给它一些思考之后,我现在认为它可能会更有意义创建多个的DataContext类,每一个涉及到在我的数据库中的特定空间或逻辑相互关联的部分。我最关心的是,实例化和布置一个巨大的DataContext类所有的时间,涉及到数据库的特定区域将实行对应用程序资源的不必要征收个人操作。此外,它更容易创建和管理不是一大一小的DataContext文件。我会失去的东西是会有那会不会是通航通过LinqToSql(即使链关系的连接它们在实际的数据库)数据库的某个遥远的部分。此外,将有一些,将存在于一个以上的DataContext表类。

However, after giving it some thought, I am now thinking that it may make more sense to create multiple DataContext classes, each one relating to a specific namespace or logical interrelated section within my database. My main concern is that instantiating and disposing one huge DataContext class all the time for individual operations that relate to specific areas of the Database would be impose an unnecessary imposition on application resources. Additionally, it is easier to create and manage smaller DataContext files than one big one. The thing that I would lose is that there would be some distant sections of the database that would not be navigable through LinqToSql (even though a chain of relationships connects them in the actual database). Additionally, there would be some table classes that would exist in more than one DataContext.

在多个DataContexts(对应于DB命名空间)是否适合代替(或补充)的一个非常大的DataContext类(对应于整个数据库)?

Any thoughts or experience on whether multiple DataContexts (corresponding to DB namespaces) are appropriate in place of (or in addition to) one very large DataContext class (corresponding to the whole DB)?

推荐答案

我不同意约翰的回答。 DataContext的(或LINQ到实体的ObjectContext)更是一个工作单位比连接。它管理变化的跟踪,等看到这个博客帖子描述:

I disagree with John's answer. The DataContext (or Linq to Entities ObjectContext) is more of a "unit of work" than a connection. It manages change tracking, etc. See this blog post for a description:

<一个href="http://blogs.msdn.com/dinesh.kulkarni/archive/2008/04/27/lifetime-of-a-linq-to-sql-datacontext.aspx">Lifetime一个LINQ to SQL中的DataContext 的

本博客文章的四个要点是DataContext的:

The four main points of this blog post are that DataContext:

  1. 是非常适合 一个工作单位的做法
  2. 也被设计为 无状态服务器操作
  3. 是不适合     长寿命使用
  4.     使用时应非常小心的     任何SumbitChanges()操作。
  1. Is ideally suited for a "unit of work" approach
  2. Is also designed for "stateless" server operation
  3. Is not designed for Long-lived usage
  4. Should be used very carefully after any SumbitChanges() operation.

考虑到,我不认为使用一个以上的DataContext会做任何事实harm-,创建不同的DataContexts针对不同类型的工作,将有助于使你的LinqToSql impelmentation更usuable和组织。唯一的缺点是,你将无法sqlmetal使用自动生成的dmbl。

Considering that, I don't think using more than one DataContext would do any harm- in fact, creating different DataContexts for different types of work would help make your LinqToSql impelmentation more usuable and organized. The only downside is you wouldn't be able to use sqlmetal to auto-generate your dmbl.

这篇关于多个DataContext的类有史以来合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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