Are Multiple DataContext classes ever appropriate?

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:

Lifetime of a LINQ to SQL DataContext

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

  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.
    

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)