Convert Table Column Into C# Model Class

In this blog, I will demonstrate how to convert a table column into a C# model class, using stored procedure. This is a very useful phenomenon for most of the C# programmers.

SQL Code

Create table and column as you need, like given below.

  1. CREATE TABLE [dbo].[EmployeeMaster](  
  2.     [RowId] [bigint] NULL,  
  3.     [EmpFirstName] [varchar](50) NULL,  
  4.     [EmpLastName] [varchar](50) NULL,  
  5.     [PhoneNo] [bigint] NULL,  
  6.     [City] [bigint] NULL,  
  7.     [Address] [varchar](500) NULL,  
  8.     [DateOfBirth] [datetime] NULL,  
  9.     [Gender] [int] NULL,  
  10.     [MaritalStatus] [bit] NULL,  
  11.     [EmpStatus] [bit] NULL  
  12. ) ON [PRIMARY]  
  13. GO  

Usually, you can create class model with the same name as table columns. Often, you look at the table structure and then create property. But now, you just have to pass the table name and types of class.

SQL Code

  1. CREATE PROCEDURE CREATEMODEL  
  2. (  
  3.      @TableName SYSNAME ,  
  4.      @CLASSNAME VARCHAR(500)   
  5. )  
  6. AS  
  7. BEGIN  
  8.     DECLARE @Result VARCHAR(MAX)  
  9.   
  10.     SET @Result = @CLASSNAME + @TableName + '  
  11. {'  
  12.   
  13. SELECT @Result = @Result + '  
  14.     public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }'  
  15. FROM  
  16. (  
  17.     SELECT   
  18.         REPLACE(col.NAME, ' ''_') ColumnName,  
  19.         column_id ColumnId,  
  20.         CASE typ.NAME   
  21.             WHEN 'bigint' THEN 'long'  
  22.             WHEN 'binary' THEN 'byte[]'  
  23.             WHEN 'bit' THEN 'bool'  
  24.             WHEN 'char' THEN 'string'  
  25.             WHEN 'date' THEN 'DateTime'  
  26.             WHEN 'datetime' THEN 'DateTime'  
  27.             WHEN 'datetime2' then 'DateTime'  
  28.             WHEN 'datetimeoffset' THEN 'DateTimeOffset'  
  29.             WHEN 'decimal' THEN 'decimal'  
  30.             WHEN 'float' THEN 'float'  
  31.             WHEN 'image' THEN 'byte[]'  
  32.             WHEN 'int' THEN 'int'  
  33.             WHEN 'money' THEN 'decimal'  
  34.             WHEN 'nchar' THEN 'char'  
  35.             WHEN 'ntext' THEN 'string'  
  36.             WHEN 'numeric' THEN 'decimal'  
  37.             WHEN 'nvarchar' THEN 'string'  
  38.             WHEN 'real' THEN 'double'  
  39.             WHEN 'smalldatetime' THEN 'DateTime'  
  40.             WHEN 'smallint' THEN 'short'  
  41.             WHEN 'smallmoney' THEN 'decimal'  
  42.             WHEN 'text' THEN 'string'  
  43.             WHEN 'time' THEN 'TimeSpan'  
  44.             WHEN 'timestamp' THEN 'DateTime'  
  45.             WHEN 'tinyint' THEN 'byte'  
  46.             WHEN 'uniqueidentifier' THEN 'Guid'  
  47.             WHEN 'varbinary' THEN 'byte[]'  
  48.             WHEN 'varchar' THEN 'string'  
  49.             ELSE 'UNKNOWN_' + typ.NAME  
  50.         END ColumnType,  
  51.         CASE   
  52.             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')   
  53.             THEN '?'   
  54.             ELSE ''   
  55.         END NullableSign  
  56.     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  
  57.     where object_id = object_id(@TableName)  
  58. ) t  
  59. ORDER BY ColumnId  
  60. SET @Result = @Result  + '  
  61. }'  
  62.   
  63. print @Result  
  64.   
  65. END  

After running this procedure, execute SP with parameters. Now, the result will be loaded as model class.

  1. exec CREATEMODEL 'EmployeeMaster''public class '  

Output

  1. public class EmployeeMaster  
  2. {  
  3. public long? RowId { getset; }  
  4. public string EmpFirstName { getset; }  
  5. public string EmpLastName { getset; }  
  6. public long? PhoneNo { getset; }  
  7. public long? City { getset; }  
  8. public string Address { getset; }  
  9. public DateTime? DateOfBirth { getset; }  
  10. public int? Gender { getset; }  
  11. public bool? MaritalStatus { getset; }  
  12. public bool? EmpStatus { getset; }  
  13. }