Welcome to the CodeSmith Community!

Re: User Defined types in ColumnSchema object

Support Forums

Feel free to ask any questions about CodeSmith here.

User Defined types in ColumnSchema object

Answered (Verified) This question is answered

I just purchased CodeSmith 5 pro after using 4 pro.  Many of my templates use the ColumnSchema.NativeType to handle code generation for UDT's in SQL Server.  This seems to be broken in Codesmith 5.  If I look at the NativeType property of a ColumnSchema it returns the name of the SQL Server type (char) rather my my user defined type (tYesNo) that is used in the definition.  This breaks LOTS of things.

How do I get the UDT type name of the filed in CodeSmith 5???  This worked fine in 4

  • Post Points: 35
Verified Answer
  • Hello,

    I'd recommend using the following code which has been tested and you can just update your existing code with Parenthesis (). It makes sense that the NativeType should be the underlying type as it is "native" where the UDT is just an alias type. For example if all of your templates are checking the native type to for specific functionality they wouldn't know how to intemperate a random UDT Type. If you sent your template to someone else, it wouldn't work as expected either. I believe this was the old behavior: would be the native-type equals udt which resolves to an undefined system-type. Now the native type equals the real native type and the system type resolves from the real native type and not to DbType.Object. 

    <%@ Template Language="VB" TargetLanguage="VB" Description="An example on creating a class with properties from a database table." %>
    <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="DataSource" Optional="False" %>
    <%@ Assembly Name="SchemaExplorer" %>
    <%@ Import Namespace="SchemaExplorer" %>
    
    <% For Each column as ColumnSchema in SourceTable.Columns %>
        <%= column.NativeType() %>
    <% Next %>
    
    <script runat="template">
    Public Shared Function NativeType(ByVal column As ColumnSchema) As String
        If(column.ExtendedProperties.Contains("CS_UserType")) Then
            Return column.ExtendedProperties("CS_UserType").Value.ToString()
        End If
    
        Return column.NativeType
    End Function
    </script>

    Thanks

    -Blake Niemyjski

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

All Replies
  • Hello,

    Thanks for upgrading and we apologize for any headaches this may have caused. We don't make a change like this unless the benefits greatly outweight the negatives. The change was requested by quite a few users because templates needed additional custom logic  (per users setup) to detect a UDT's base type and generate accordingly. We've added new ExtendedProperties for this information. For ColumnSchema and ViewSchema you should check the ExtendedProperties for CS_UserType. Please use the following Pseudo code as an example on how you could update your templates.

    // Extension Method
    public static string ToNativeType(this ColumnSchema column)
    {
      if(column.ExtendedProperties != null && column.ExtendedProperties.ContainsKey("CS_UserType")
         return column.ExtendedProperties["CS_UserType"].Value.ToString();
    
      return column.NativeType;
    }
    
    // Method
    private string ToNativeType(ColumnSchema column)
    {
      if(column.ExtendedProperties != null && column.ExtendedProperties.ContainsKey("CS_UserType")
         return column.ExtendedProperties["CS_UserType"].Value.ToString();
    
      return column.NativeType;
    }

    Another work around would be to recompile the SqlSchemaProvider to use the existing logic.

    You would need to update lines 240 and 710 of the SqlSchemaProvider.cs file. The source code is located in your My Documents folder: Documents\CodeSmithGenerator\Samples\v5.3\Projects\CSharp\SqlSchemaProvider. Please note, that you would need to fix the references before compiling and place the assembly in the correct directory under the CodeSmith Generator Program Files folder.

    Please let us know if you have any questions.

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

  • Blake,

    Thanks for the quick response.

    For some reason the ExtendedProperties in my code does not have a ContainsKey method. The way I got it to work is as follows:

    Public Function ToNativeType(TheColumn As ColumnSchema) As String

       Dim SQLType as String

       Try

           SQLType = TheColumn.ExtendedProperties("CS_UserType").Value.ToString()

       Catch

           SQLType = TheColumn.NativeType

       End Try

       Return SQLType

    End Function

    Yes, it has to throw an exception, but without ContainsKey (and I could not get Contains to work either), I didn't know a better approach.

    <RANT> Why was this implemented as a breaking change?  Any prior code would have continued to work and people that needed the base data type could have used another (new) property that resolved to the SQL data type since they didn't have that ability in the prior version.  My choices are to change every template or change the CS source code from the standard (fun for future updates)

    </RANT>

  • Hello,

    I'd recommend using the following code which has been tested and you can just update your existing code with Parenthesis (). It makes sense that the NativeType should be the underlying type as it is "native" where the UDT is just an alias type. For example if all of your templates are checking the native type to for specific functionality they wouldn't know how to intemperate a random UDT Type. If you sent your template to someone else, it wouldn't work as expected either. I believe this was the old behavior: would be the native-type equals udt which resolves to an undefined system-type. Now the native type equals the real native type and the system type resolves from the real native type and not to DbType.Object. 

    <%@ Template Language="VB" TargetLanguage="VB" Description="An example on creating a class with properties from a database table." %>
    <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="DataSource" Optional="False" %>
    <%@ Assembly Name="SchemaExplorer" %>
    <%@ Import Namespace="SchemaExplorer" %>
    
    <% For Each column as ColumnSchema in SourceTable.Columns %>
        <%= column.NativeType() %>
    <% Next %>
    
    <script runat="template">
    Public Shared Function NativeType(ByVal column As ColumnSchema) As String
        If(column.ExtendedProperties.Contains("CS_UserType")) Then
            Return column.ExtendedProperties("CS_UserType").Value.ToString()
        End If
    
        Return column.NativeType
    End Function
    </script>

    Thanks

    -Blake Niemyjski

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

Page 1 of 1 (4 items)