Skip to main content

SQL Script To Generate C# Model

While working on a data migration project that uses C# and Azure Functions.  I found myself in the need to generate a model of the table(s).  While doing a Google search I came across the following blog post that has a script to generate the model properties for me.  Simply replace the table name variable at the top of the script with the table name you want to generate the properties for.  You may also need to include the schema name in the table name if you have multiple tables with the same name, just under different schema's.  I did make one change to it in the select @ result line.  It was adding a blank line in between each property, so I removed the new line.


 declare @TableName sysname = 'TABLE_NAME'  
 declare @Result varchar(max) = 'public class ' + @TableName + '  
 {'  
 select @Result = @Result + '  
   public ' + (CASE WHEN ColumnName = 'RowVersion' THEN 'byte[]' ELSE ColumnType END) + NullableSign + ' ' + ColumnName + ' { get; set; }'  
 from  
 (  
   select   
     replace(col.name, ' ', '_') ColumnName,  
     column_id ColumnId,  
     case typ.name   
       when 'bigint' then 'long'  
       when 'binary' then 'byte[]'  
       when 'bit' then 'bool'  
       when 'char' then 'string'  
       when 'date' then 'DateTime'  
       when 'datetime' then 'DateTime'  
       when 'datetime2' then 'DateTime'  
       when 'datetimeoffset' then 'DateTimeOffset'  
       when 'decimal' then 'decimal'  
       when 'float' then 'float'  
       when 'image' then 'byte[]'  
       when 'int' then 'int'  
       when 'money' then 'decimal'  
       when 'nchar' then 'string'  
       when 'ntext' then 'string'  
       when 'numeric' then 'decimal'  
       when 'nvarchar' then 'string'  
       when 'real' then 'double'  
       when 'smalldatetime' then 'DateTime'  
       when 'smallint' then 'short'  
       when 'smallmoney' then 'decimal'  
       when 'text' then 'string'  
       when 'time' then 'TimeSpan'  
       when 'timestamp' then 'timestamp'  
       when 'rowversion' then 'byte[]'  
       when 'tinyint' then 'byte'  
       when 'uniqueidentifier' then 'Guid'  
       when 'varbinary' then 'byte[]'  
       when 'varchar' then 'string'  
       else 'UNKNOWN_' + typ.name  
     end ColumnType,  
     case   
       when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')   
       then '?'   
       else ''   
     end NullableSign  
   from sys.columns col  
     join sys.types typ on  
       col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id  
   where object_id = object_id(@TableName)  
 ) t  
 order by ColumnId  
 set @Result = @Result + '  
 }'  
 print @Result  

Reference:
Habilis. (2017, May 01). Creating C# model class from SQL query. Retrieved from https://habilisbest.com/creating-c-model-class-from-sql-query

Comments

Post a Comment

Popular posts from this blog

Validating User Input In CRM Portals With JavaScript

When we are setting up CRM Portals to allow customers to update their information, open cases, fill out an applications, etc. We want to make sure that we are validating their input before it is committed to CRM.  This way we ensure that our data is clean and meaningful to us and the customer. CRM Portals already has a lot validation checks built into it. But, on occasion we need to add our own.  To do this we will use JavaScript to run the validation and also to output a message to the user to tell them there is an issue they need to fix. Before we can do any JavaScript, we need to check and see if we are using JavaScript on an Entity Form or Web Page.  This is because the JavaScript, while similar, will be different.  First, we will go over the JavaScript for Entity Forms.  Then, we will go over the JavaScript for Web Pages.  Finally, we will look at the notification JavaScript. Entity Form: if (window.jQuery) { (function ($) { if ...

Dynamics Set IFrame URL - D365 v8 vs. D365 v9

While doing client work, I came across a problem with setting an IFrame URL dynamically.  The underlying issue was that the sandbox instance is on v8 of Dynamics 365 and production is on v9 of Dynamics 365.  The reason for this was because this client was setup around the time that Microsoft rolled out v9.  Anyways, JavaScript that I wrote to dynamically set the URL of the IFrame wasn't working in the v9 instance.  This was because of changes that Microsoft made to how IFrames are loaded on the form and also changes to JavaScript. Here is my v8 setup: JavaScript runs OnLoad of contact form.  This works because of how IFrames are loaded in v8.  You can also run it on either a tab change (hide / show) or OnReadyStateComplete event of the IFrame.  Depending on your setup you will need to choose which is best for you.  For me in this case it was the OnLoad event. Here is the JavaScript: function OnLoad() { //Get memberid var...

Effective Logging in Microsoft Dynamics 365 Plugins: Best Practices and Examples

Microsoft Dynamics 365 is a powerful suite of business applications that provides organizations with tools for managing customer relationships, sales, and operations. One of the essential aspects of developing custom plugins for Dynamics 365 is proper logging to ensure smooth functionality and easy debugging. In this blog post, we'll discuss best practices for logging in Microsoft Dynamics 365 plugins and provide examples to help you implement effective logging in your custom solutions. Understanding the Plugin Trace Log Microsoft Dynamics 365 provides a built-in logging mechanism called the Plugin Trace Log. The Plugin Trace Log can be used to record custom messages, exceptions, and other information for debugging purposes. To enable the Plugin Trace Log, follow these steps: Navigate to Settings > Administration > System Settings. Under the Customization tab, locate the "Plugin and Custom Workflow Activity Tracing" section. Set the option to "All" or ...