为了在 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:
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 类是否合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!