Version 1.0 - Project Description

Visual Studio Extension for faster automatic generation C# or Visual Basic Linq2Sql classes based on config files. Extension also allows define modifications. It uses Sqlmetal.exe for generating same classes as normal dbml.

Please give me feedback in discussion, thanks!


You must have installed Windows SDK, which contain SqlMetal.exe. Please check folder C:\Program Files\Microsoft SDKs\Windows\v7.0 (or v6.0A) \Bin.


In Visual Studio Tools -> External Tools... click on Add. Set Title to DBML Updater and in Command find DbmlUpdater.exe on your harddrive. Next set $(ProjectDir) as Initial Directory and check Use Output widnow. Setup screenshot

How it works

Very simple. In working directory (which was specified in Install) find all *.dbml.xml files in all subdirectories. So if you have configuration file MyData.dbml.xml, on the end you will have MyData.dbml, MyData.dbml.xml and MyData.designer.cs files.

Configuration file

  • DBML
    • generate = true/false - If set to true, existing dbml file will be overridden; if false, dbml file must exists
    • connectionString = string - do not use connection string to access mdf file directly
    • views = true/false optional - extracts database views
    • functions = true/false optional - extracts database functions
    • sprocs = true/false optional - extracts stored procedures
  • Changes
    • Edit
      • xpath = string - determines path to element
      • Attr
        • name = string - attribute name of element (found by xpath)
        • value = string - new value of attribute
    • Delete
      • xpath = string - determines path to element
  • Code
    • generate = true/false - If set to true, existing cs/vb file will be overridden; if false, you must open and save dbml file in Vistual studio to generate code
    • lang = cs/vb - source code language
    • namespace = string optional - default is no namespace
    • context = string optional - if specified, "Context" is appended on the end of string; if not specified, derived from database name

Add new configuration file to project

In context menu on the folder in Solution explorer click on "New item..." and select "XML File". Then enter new name with ".dbml.xml" extension.

Sample of configuration file 'Text.dbml.xml'

<?xml version="1.0" encoding="utf-8" ?>
  <DBML generate="true" connectionString="Data Source=.\SQLEXPRESS; User ID=sa; Password=P@ssw0rd; Initial catalog=DB" views="true" functions="false" sprocs="false" />
    <Edit xpath="ns1:Database/ns1:Table[@Name='dbo.Table1']/ns1:Type/ns1:Association[@ThisKey='Table2_ID_ContactAddress']">
      <Attr name="Member" value="ContactAddress" />
    <Edit xpath="ns1:Database/ns1:Table[@Name='dbo.Table1']/ns1:Type/ns1:Association[@ThisKey='Table2_ID_InvoiceAddress']">
      <Attr name="Member" value="InvoiceAddress" />
    <Edit xpath="ns1:Database/ns1:Table[@Name='dbo.Table1']/ns1:Type/ns1:Association[@ThisKey='Table2_ID_RegisteredAddress']">
      <Attr name="Member" value="RegisteredAddress" />
    <Delete xpath="ns1:Database/ns1:Table[@Name='dbo.Table2']/ns1:Type/ns1:Association[@Name='FK_Table1_Table2_ContactAddress']" />
    <Delete xpath="ns1:Database/ns1:Table[@Name='dbo.Table2']/ns1:Type/ns1:Association[@Name='FK_Table1_Table2_InvoiceAddress']" />
    <Delete xpath="ns1:Database/ns1:Table[@Name='dbo.Table2']/ns1:Type/ns1:Association[@Name='FK_Table1_Table2_RegisteredAddress']" />
  <Code generate="true" namespace="DbmlUpdater.Data" context="MyData" />

First run

After the first successful run, you must add dbml, using "Existing item..." in context menu, file into project. After adding dbml file, you must run it again, because file is changed by Visual Studio (i don't know why). And I recommend to delete the file ".dbml.layout".

Output window - allow you to check errors

 DBML Updater v1.0
  - Generating DBML file... OK
  - Checking DBML file exists... OK
  - Modifing DBML file
      - [0] editing... OK
      - [1] editing... OK
      - [2] editing... OK
      - [3] deleting... OK
      - [4] deleting... OK
      - [5] deleting... OK
  - Generating Code file... OK

                                                           17.06.2011 12:57:02

That's all

When you change database, simply run this tool and DBML and code will be generated including specified modifications.

Pavel ViPPeR Pech

Last edited Feb 10, 2012 at 12:08 PM by vipper_cnx, version 1


No comments yet.