多个 DataContext 类是否合适?

Are Multiple DataContext classes ever appropriate?(多个 DataContext 类是否合适?)
本文介绍了多个 DataContext 类是否合适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

为了在 ASP.net 3.5 应用程序中充分使用 LinqToSql,需要创建 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 生成的外键连接来轻松地在我的代码中的相关对象之间切换,插入相关对象等.

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 文件比创建和管理大的 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 类(对应于整个 DB)是否有任何想法或经验?

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 to Entities 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:

LINQ to SQL DataContext 的生命周期

这篇博文的四个要点是 DataContext:

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

  1. 非常适合对于工作单元"方法
  2. 还专为无状态"服务器操作
  3. 并非专为长期使用
Should be used very carefully after
any SumbitChanges() operation.

考虑到这一点,我认为使用多个 DataContext 不会有任何危害——事实上,为不同类型的工作创建不同的 DataContext 将有助于使您的 LinqToSql 实现更易于使用和更有条理.唯一的缺点是您将无法使用 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 类是否合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)