How-to: Preserve column names in generated PLINQO entities.

 

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();
        }
    }
}

 


Posted Jul 27 2010, 09:39 AM by Blake Niemyjski |


Comments 

Top 50 Contributor
Joined on 09-30-2010
United Kingdom and United States
re: How-to: Preserve column names in generated PLINQO entities. Posted on Sep 30 2010, 09:17 AM

Thanks for the great example code!  I can use this to correct my occassional inadvertent misspelling of column names moving databases from American English to British English. Another great usageof this is to correct table names that use code key words.

windowscoding.com;
Copyright © 2008 Windows Coding
Microsoft and Microsoft logo's are trademarks of Microsoft Corporation.