There are two ways to work when using ASP.NET Dynamic Data:
- Automatically Scaffold everything
- 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.
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.
- Download Damien Guard’s LINQ to SQL templates. You will only need the L2ST4.ttinclude file.
- Create a MetaData folder in your solution
- Add new new file and call it MetaData.tt (you might be prompted with a warning - click OK)
- Paste the following code in:
<#@ template language="C#v3.5" hostspecific="True"#> <#@ include file="../L2ST4.ttinclude"#> <#@ output extension=".generated.cs"#> <# // Template by Eric Phan http://ericphan.info inspired by Damien Guard http://damieng.com var data = new Data(Host.TemplateFile.Replace("MetaData.tt","../NAMEOFYOURDBML.dbml")); var code = new CSharpCodeLanguage(); data.Serialization = SerializationMode.DataContractSP1; #> #pragma warning disable 1591 //------------------------------------------------------------------------------ //
//------------------------------------------------------------------------------ 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
// 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.
// - RIght click on MetaData.tt and click Run Custom Tool
- Look at the MetaData.generated.cs file
Note: MetaData.tt 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)