2 minute read

There may come a time where you need to rename a column in your generated entity for whatever reason. The following tip and trick article will show you how to quickly accomplish this.

Let’s assume you have a generated property for the column AccountId and it is defined like this:

[System.CodeDom.Compiler.GeneratedCode("CodeSmith", "5.0.0.0")]
private int _accountId = default(int);

/// <summary>
/// Gets the AccountId column value.
/// </summary>
[System.Data.Linq.Mapping.Column(Name = "AccountId", Storage = "_accountId", DbType = "int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)]
[System.Runtime.Serialization.DataMember(Order = 1)]
[System.CodeDom.Compiler.GeneratedCode("CodeSmith", "5.0.0.0")]
public int AccountId
{
    get { return _accountId; }
    set
    {
        if (_accountId != value)
        {
            OnAccountIdChanging(value);
            SendPropertyChanging("AccountId");
            _accountId = value;
            SendPropertyChanged("AccountId");
            OnAccountIdChanged();
        }
    }
}

If you take a look at the DBML file, it will look like this:

<Type Name="Account">
  <Column Name="AccountId" Storage="_accountId" Type="System.Int32" DbType="int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
</Type>

Say you want to change the name of the property and have it preserved across generation. You will need to open the generated DBML file with the designer and change the name. I recommend always opening up the DBML file with an Xml Text editor (right click on the dbml file and select open with) and update the definition like this.

Please note that opening the DBML file in the class designer will cause custom DBML data to be lost!

<Type Name="Account">
  <Column Name="AccountId" Member="Id" Storage="_id" Type="System.Int32" DbType="int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
</Type>

Notice that an Member Attribute was added and the Storage Attribute value was changed. These are the two attributes that you will need to modify in order to rename properties in the DBML file. If you regenerate and take a look at the generated partial class. You will see that the AccountID property was dropped and the Id Property was created:

[System.CodeDom.Compiler.GeneratedCode("CodeSmith", "5.0.0.0")]
private int _id = default(int);

/// <summary>
/// Gets the AccountId column value.
/// </summary>
[System.Data.Linq.Mapping.Column(Name = "AccountId", Storage = "_id", DbType = "int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)]
[System.Runtime.Serialization.DataMember(Order = 12)]
[System.CodeDom.Compiler.GeneratedCode("CodeSmith", "5.0.0.0")]
public int Id
{
    get { return _id; }
    set
    {
        if (_id != value)
        {
            OnIdChanging(value);
            SendPropertyChanging("Id");
            _id = value;
            SendPropertyChanged("Id");
            OnIdChanged();
        }
    }
}

Join the mailing list

Get notified of new posts and related content to your inbox. I will never sell you anything and I will NEVER sell your email address.