<p>In order to fully use LinqToSql in an ASP.net 3.5 application, it is necessary to create <a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx" rel="noreferrer">DataContext</a> <a href="http://dotnetslackers.com/articles/csharp/InsideTheLINQToSQLDataContextClass.aspx" rel="noreferrer">classes</a> (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.</p>

<p>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.</p>

<p>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.</p>

<p>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)?</p>

[{'answer_id': 2050, 'author': 'John Downey', 'author_id': 200, 'author_profile': 'https://Stackoverflow.com/users/200', 'pm_score': 2, 'selected': False, 'text': "<p>In my experience with LINQ to SQL and LINQ to Entities a DataContext is synonymous to a connection to the database. So if you were to use multiple data stores you would need to use multiple DataContexts. My gut reaction is you wouldn't notice to much of a slow down with a DataContext that encompasses a large number of tables. If you did however you could always split the database logically at points where you can isolate tables that don't have any relationship to other sets of tables and create multiple contexts.</p>\n"}, {'answer_id': 2950, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 3, 'selected': False, 'text': "<p>I'd been wrangling over the same question whilst retro fitting LINQ to SQL over a legacy DB. Our database is a bit of a whopper (150 tables) and after some thought and experimentation I elected to use multiple DataContexts. Whether this is considered an anti-pattern remains to be seen, but for now it makes life manageable.</p>\n"}, {'answer_id': 5310, 'author': 'Evan', 'author_id': 667, 'author_profile': 'https://Stackoverflow.com/users/667', 'pm_score': 6, 'selected': True, 'text': '<p>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:</p>\n\n<p><a href="http://blogs.msdn.com/dinesh.kulkarni/archive/2008/04/27/lifetime-of-a-linq-to-sql-datacontext.aspx" rel="noreferrer">Lifetime of a LINQ to SQL DataContext</a></p>\n\n<p>The four main points of this blog post are that DataContext:</p>\n\n<ol>\n<li>Is ideally suited\nfor a "unit of work" approach </li>\n<li>Is also designed for\n"stateless" server operation</li>\n<li>Is not designed for\n    Long-lived usage</li>\n<li><pre><code>Should be used very carefully after\nany SumbitChanges() operation.\n</code></pre></li>\n</ol>\n\n<p>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. </p>\n'}, {'answer_id': 29293, 'author': 'liammclennan', 'author_id': 2785, 'author_profile': 'https://Stackoverflow.com/users/2785', 'pm_score': 2, 'selected': False, 'text': '<p>I think John is correct. </p>\n\n<p>"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"</p>\n\n<p>How do you support that statement? What is your experiment that shows that a large DataContext is a performance bottleneck? Having multiple datacontexts is a lot like having multiple databases and makes sense in similar scenarios, that is, hardly ever. If you are working with multiple datacontexts you need to keep track of which objects belong to which datacontext and you can\'t relate objects that are not in the same data context. That is a costly design smell for no real benefit. </p>\n\n<p>@Evan "The DataContext (or Linq to Entities ObjectContext) is more of a "unit of work" than a connection"\nThat is precisely why you should not have more than one datacontext. Why would you want more that one "unit of work" at a time?</p>\n'}, {'answer_id': 24273101, 'author': 'Jordan Rieger', 'author_id': 284704, 'author_profile': 'https://Stackoverflow.com/users/284704', 'pm_score': 2, 'selected': False, 'text': '<p>I have to disagree with the accepted answer. In the question posed, the system has a single large database with strong foreign key relationships between almost every table (also the case where I work). In this scenario, breaking it up into smaller DataContexts (DC\'s) has two immediate and major drawbacks (both mentioned by the question):</p>\n\n<ol>\n<li><strong>You lose relationships between some tables.</strong> You can try to choose your DC boundaries wisely, but you will eventually run into a situation where it would be very convenient to use a relationship from a table in one DC to a table in another, and you won\'t be able to.</li>\n<li><strong>Some tables may appear in multiple DC\'s.</strong> This means that if you want to add table-specific helper methods, business logic, or other code in partial classes, the types won\'t be compatible across DC\'s. You can work around this by inheriting each entity class from its own specific base class, which gets messy. Also, schema changes will have to be duplicated across multiple DC\'s.</li>\n</ol>\n\n<p>Now those are significant drawbacks. Are there advantages big enough to overcome them? The question mentions performance:</p>\n\n<blockquote>\n  <p>My main concern is that instantiating and disposing one huge\n  DataContext class all the time for individual operations that relate\n  to specific areas of the Database would be impose an unnecessary\n  imposition on application resources.</p>\n</blockquote>\n\n<p>Actually, it is not true that a large DC takes significantly more time to instantiate or use in a typical unit of work. In fact, <a href="http://blogs.msdn.com/b/gisenberg/archive/2008/05/20/linq-to-sql-optimizing-datacontext-construction-with-the-factory-pattern.aspx#9904456" rel="nofollow">after the first instance is created in a running process, subsequent copies of the same DC can be created almost instantaneously</a>.</p>\n\n<p>The only real advantage from multiple DC\'s for a single, large database with thorough foreign key relationships is that you can compartmentalize your code a little better. But you can already do this with partial classes.</p>\n\n<p>Also, the unit of work concept is not really relevant to the original question. Unit of work typically refers to how much a work a single DC <strong><em>instance</em></strong> is doing, not how much work a DC <strong><em>class</em></strong> is <strong><em>capable</em></strong> of doing.</p>\n'}]
['https://Stackoverflow.com/questions/1949', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/51/']