There are two ways to work when using ASP.NET Dynamic Data:

  1. Automatically Scaffold everything
  2. Scaffold only those tables you specify

Scaffolding manually is alright but you need to remember to add the ScaffoldTableAttribute to the new tables you want managed by ASP.NET Dynamic Data

I prefer to let it automatically scaffold all tables so as I’m developing and changing the database schema I automatically get the CRUD pages. I then choose which ones to exclude.

Note: You can scaffold all tables by setting the ScaffoldAllTables property to true in your Global.asax.cs

model.RegisterContext(typeof(TestDataContext), new ContextConfiguration() { ScaffoldAllTables = true });

This is great, until you start using the ASP.NET Membership and Role providers. When you use these (integrated into the one database) it adds a lot of aspnet_* tables to your database so when you regenerate your LINQ to Entities/SQL file.

ASPNET-MembershipTables.png DynamicData-MembershipTables.png

You can avoid these issues if you make ASP.NET generate these tables into a separate database but if you like everything in one database then there is a solution for you. T4 Templates - built right into Visual Studio. We can use these templates to automatically exclude the aspnet tables.

  1. Download Damien Guard’s LINQ to SQL templates. You will only need the L2ST4.ttinclude file.


  2. Create a MetaData folder in your solution
  3. Add new new file and call it (you might be prompted with a warning - click OK)
  4. Paste the following code in:
    		<#@ template language="C#v3.5" hostspecific="True"#>
    <#@ include file="../L2ST4.ttinclude"#>
    <#@ output extension=".generated.cs"#>
    <# // Template by Eric Phan inspired by Damien Guard
    var data = new Data(Host.TemplateFile.Replace("","../NAMEOFYOURDBML.dbml")); 
    var code = new CSharpCodeLanguage();
    data.Serialization = SerializationMode.DataContractSP1;
    #pragma warning disable 1591
    // This code was generated by LINQ to SQL template for T4 C# v0.79
    // Generated at <#=DateTime.Now#>
    // Changes to this file may cause incorrect behavior and will be lost if
    // the code is regenerated.
    //------------------------------------------------------------------------------ using System; using System.ComponentModel; using System.Data.Linq; using System.Data.Linq.Mapping; using System.ComponentModel.DataAnnotations; using <#=data.EntityNamespace#>.MetaData; namespace <#=data.EntityNamespace#>.MetaData { <# foreach(Table table in data.Tables) { foreach(TableClass class1 in table.Classes) { if (class1.Name.StartsWith("Aspnet_")) {#> [ScaffoldTable(false)]<#}#> public partial class <#=class1.Name#>MetaData {<# if (class1.Columns.Count > 0) foreach(Column column in class1.Columns) if (column.IsPrimaryKey) { #> [ScaffoldColumn(false)] public object <#=column.Member#> { get; set; } <#}#> } <#}}#>} namespace <#=data.EntityNamespace#> { <# foreach(Table table in data.Tables) { foreach(TableClass class1 in table.Classes) { #> [MetadataType(typeof(<#=class1.Name#>MetaData))] public partial class <#=class1.Name#> { } <#}}#> } #pragma warning restore 1591
  5. RIght click on and click Run Custom Tool
  6. Look at the MetaData.generated.cs file

Note: will create all partial classes based on your DBML file, create the associated metadata classes and associate them to the class. It will also add the ScaffoldTable(false) attribute to any Aspnet tables and automatically unscaffold any primary key columns (workaround for GUID issue)