This project is read-only.

DNN Platform Linq to Sql Model Adapter

More details about this project may be found on my blog.


A DataContext adapter allowing DotNetNuke modules using Linq to Sql to automatically apply an end user's database owner and object qualifier at runtime.


  • Run-time adjustment of an attributed MetaModel based on DotNetNuke database owner and object qualifier
  • Continued use of the Visual Studio Linq to Sql designer
  • No tedious changes in the compiler-generated code
  • Continued use of an attributed-meta model (and thereby no external schema deployment files)


The .NET-supplied DataContext allows development using the attributed model automatically generated by the Visual Studio designer. However, when deployed within DotNetNuke, these attributes would need to be adjusted at runtime for whatever contextual object qualifier and database owner were in use by the end-user. Unfortunately, the MetaModel used by the Linq to Sql system is overwhelmingly read-only (and, frustratingly, the AttributedMetaModel subclasses are internal and sealed) and there was no simple remedy to this problem. Until now.

This solution utilizes a two-step approach. First, an AttributedMetaModel is generated based on the attributes automatically created through the designer. Next, the model is walked to generate an adapted model, which is cached and returned (models are thread-safe). Walking is reasonably inexpensive, and since the models are cached by type, performance is not affected.

Dependency injection is used to adapt attributed entities (database, table, and function names) into their adapted counterparts. A DotNetNukeAdapter is included for this particular domain. Additional adapters could easily be constructed for other domains, should any arise (to illustrate this point, a NonTransformingAdapter is included in the release, which performs no transformation).


  1. Download the latest release
  2. Add a reference to the adapter assembly
  3. Open your DBML designer file in Visual Studio
  4. Change the base class of the DataContext to BrandonHaynes.ModelAdapter.DotNetNukeDataContext
  5. Ensure that the adapter assembly is deployed to your DotNetNuke bin directory.


Module developers must develop against a DotNetNuke installation with no object qualifier. This adapter does not store the design-time object qualifier for later removal. Any design-time database owner may be used; the design-time value is removed via a regular expression.

Feedback Appreciated!

Feedback about your experiences is needed, and greatly appreciated!

Last edited Apr 23, 2014 at 9:07 PM by BrandonHaynes, version 13