How-to: Rename your generated CSLA properties Tuesday, December 28 2010
In the following example, I'll show you how to use the CodeSmith Generator CSLA template's powerful renaming options. Many of you might have a legacy database with those ugly prefixes, or things you wish you could refactor but just can't. Luckily, you're in luck as CodeSmith Generator is going to save the day once again! By default we will strip any invalid characters from your property names like prefixed digits etc... Please note that these methods also work for renaming class names, but there are other methods that we will cover later that are is the recommended way to specify a class name.
In this example, I'm going to be renaming column names in the following database table:
CREATE TABLE [dbo].[Rename]( [RenameID] [int] NOT NULL,
[RenameName] [nvarchar](50) NOT NULL,
[RenameA] [nvarchar](50) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[RenameAge] [int] NOT NULL,
CONSTRAINT [PK_Rename] PRIMARY KEY CLUSTERED
(
[RenameID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

public System.String RenameName { get; set; }
public System.String RenameA { get; set; }
public System.String Name { get; set; }
public System.Int32 RenameAge { get; set; }
End Property
Public Property RenameName() As System.String
End Property
Public Property RenameA() As System.String
End Property
Public Property Name() As System.String
End Property
Public Property RenameAge() As System.Int32
End Property


public System.String RenameName { get; set; }
public System.String Location{ get; set; }
public System.String Name { get; set; }
public System.Int32 Age { get; set; }
End Property
Public Property RenameName() As System.String
End Property
Public Property Location() As System.String
End Property
Public Property Name() As System.String
End Property
Public Property Age() As System.Int32
End Property

GO
{
EntityNaming = EntityNaming.Singular,
TableNaming = TableNaming.Mixed,
ColumnNaming = ColumnNaming.Preserve
};
{
EntityNaming = EntityNaming.Singular,
TableNaming = TableNaming.Mixed,
ColumnNaming = ColumnNaming.RemoveTablePrefix
};
public System.String Name1 { get; set; }
public System.String RenameA { get; set; }
public System.String Name2 { get; set; }
public System.Int32 Age { get; set; }
End Property
Public Property Name1() As System.String
End Property
Public Property RenameA() As System.String
End Property
Public Property Name2 () As System.String
End Property
Public Property Age() As System.Int32
End Property
using (SqlConnection connection = new SqlConnection(ADOHelper.ConnectionString))
{
connection.Open();
using(SqlCommand command = new SqlCommand(commandText, connection))
{
command.Parameters.AddWithValue("@p_RenameID", this.Identification);
command.Parameters.AddWithValue("@p_RenameName", this.Name1);
command.Parameters.AddWithValue("@p_RenameA", this.RenameA);
command.Parameters.AddWithValue("@p_Name", this.Name2);
command.Parameters.AddWithValue("@p_RenameAge", this.Age);
//result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed.
int result = command.ExecuteNonQuery();
if (result == 0)
throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute.");
LoadProperty(_originalIdentificationProperty, this.Identification);
}
}
Using connection As New SqlConnection(ADOHelper.ConnectionString)
connection.Open()
Using command As New SqlCommand(commandText, connection)
command.Parameters.AddWithValue("@p_RenameID", Me.Identification)
command.Parameters.AddWithValue("@p_RenameName", Me.Name1)
command.Parameters.AddWithValue("@p_RenameA", Me.RenameA)
command.Parameters.AddWithValue("@p_Name", Me.Name2)
command.Parameters.AddWithValue("@p_RenameAge", Me.Age)
'result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed.
Dim result As Integer = command.ExecuteNonQuery()
If (result = 0) Then
throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute.")
End If
End Using
LoadProperty(_originalIdentificationProperty, Me.Identification)
End Using

