Testing your Linq to Sql Mappings

Here's a quick way to verify that all of your Linq to Sql entities are mapped to their tables correctly:

namespace DbTests {
  using System.Linq;
  using NUnit.Framework;
 
  [TestFixture]
  public class MappingTester {
    [Test]
    public void Verify_Mappings() {
      using (var context = new MyDataContext()) {
        foreach(var mappedTable in context.Mapping.GetTables().Select(x => x.RowType.Type)) {
          context.GetTable(mappedTable).Cast<object>().Take(0).ToList();
        }
      }
    }
  }
}
Written on April 21, 2009