Last active
February 28, 2024 06:51
-
-
Save RickyLin/151de5aff5166512e92eb0f616050f64 to your computer and use it in GitHub Desktop.
spGenerateEntity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER PROCEDURE [dbo].[spGenerateEntity] | |
@TableName AS NVARCHAR(128), | |
@NewDateTypes AS BIT = 0 | |
AS | |
SELECT CASE WHEN COLUMN_NAME = TABLE_NAME + 'Id' THEN '[Column("' + COLUMN_NAME + '")] | |
public ' ELSE 'public ' END | |
+ CASE DATA_TYPE WHEN 'int' THEN 'int' | |
WHEN 'money' THEN 'decimal' | |
WHEN 'varchar' THEN 'string' | |
WHEN 'datetime' THEN 'DateTime' | |
WHEN 'smalldatetime' THEN 'DateTime' | |
WHEN 'date' THEN CASE ISNULL(@NewDateTypes, 0) WHEN 0 THEN 'DateTime' ELSE 'DateOnly' END | |
WHEN 'char' THEN 'string' | |
WHEN 'bit' THEN 'bool' | |
WHEN 'numeric' THEN 'decimal' | |
WHEN 'nvarchar' THEN 'string' | |
WHEN 'decimal' THEN 'decimal' | |
WHEN 'varbinary' THEN 'byte[]' END | |
+ CASE WHEN IS_NULLABLE = 'yes' AND DATA_TYPE NOT IN ('char', 'varchar', 'nvarchar') THEN '?' ELSE '' END | |
+ ' ' | |
+ CASE WHEN COLUMN_NAME = TABLE_NAME + 'Id' THEN 'Id' | |
WHEN LEN(COLUMN_NAME) >= 2 AND RIGHT(COLUMN_NAME, 2) = 'ID' THEN LEFT(COLUMN_NAME, LEN(COLUMN_NAME) - 2) + 'Id' | |
ELSE COLUMN_NAME END | |
+ ' { get; set; }', DATA_TYPE, IS_NULLABLE | |
FROM INFORMATION_SCHEMA.COLUMNS | |
WHERE TABLE_NAME = @TableName | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment