Last active
March 21, 2025 21:34
-
-
Save sean-gilliam/b2fbb3b0a5b64bc541ab5c6760824ca9 to your computer and use it in GitHub Desktop.
Entity Framework POCO generator
This file contains hidden or 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
-- from https://gist.github.com/joey-qc/6710702 | |
-- USER SETTABLE | |
declare @tableNameForPOCO varchar(200)--limits POCO class generation to only mentioned table(s) | |
--SET @tableNameForPOCO = 'Table1,Table2,Table3'--@tableNameForPOCO can be null to mean all tables | |
--or it can be set to a comma-delimited list of table names | |
--there should be no space(s) before and after each comma in this list | |
set @tableNameForPOCO = 'TABLE_NAME' | |
-- | |
-- LOOK BUT DONT TOUCH BEYOND THIS POINT | |
declare @schema varchar(200) | |
declare @tableName varchar(200) | |
declare @columnName varchar(200) | |
declare @nullable varchar(50) | |
declare @datatype varchar(50) | |
declare @maxlen int | |
declare @pos int | |
declare @Stype varchar(50) | |
declare @isnullable varchar(1) | |
declare @Sproperty varchar(200) | |
---START of new logic to limit to certain tables only--- | |
declare table_cursor cursor for | |
select TABLE_SCHEMA, TABLE_NAME from [INFORMATION_SCHEMA].[TABLES] where | |
case | |
when ISNULL(@tableNameForPOCO, TABLE_NAME) like ('%,' + TABLE_NAME + ',%') then 1 | |
when ISNULL(@tableNameForPOCO, TABLE_NAME) like (TABLE_NAME + ',%') then 1 | |
when ISNULL(@tableNameForPOCO, TABLE_NAME) like ('%,' + TABLE_NAME) then 1 | |
when ISNULL(@tableNameForPOCO, TABLE_NAME) like TABLE_NAME then 1 | |
else 0 | |
end = 1 | |
---END of new logic to limit to certain tables only--- | |
open table_cursor | |
fetch next from table_cursor into @schema, @tableName | |
while @@FETCH_STATUS = 0 | |
begin | |
print '[Table("' + @schema + '.' + @tableName + '")]' | |
print 'public class ' + @tableName | |
print '{' | |
declare column_cursor cursor for | |
select COLUMN_NAME | |
, IS_NULLABLE | |
, DATA_TYPE | |
, isnull(CHARACTER_MAXIMUM_LENGTH,'-1') | |
, ORDINAL_POSITION | |
, case when (IS_NULLABLE = 'NO') then '' else | |
case when (DATA_TYPE IN ('char','nchar','varchar','nvarchar')) then '' else '?' end | |
end | |
from [INFORMATION_SCHEMA].[COLUMNS] | |
where [TABLE_NAME] = @tableName | |
order by [ORDINAL_POSITION] | |
open column_cursor | |
fetch next from column_cursor into @columnName, @nullable, @datatype, @maxlen, @pos, @isNullable | |
while @@FETCH_STATUS = 0 | |
begin | |
select @sType = | |
case @datatype when 'int' then 'int' | |
when 'smallint' then 'short' | |
when 'bigint' then 'long' | |
when 'decimal' then 'decimal' | |
when 'money' then 'decimal' | |
when 'char' then 'string' | |
when 'nchar' then 'string' | |
when 'varchar' then 'string' | |
when 'nvarchar' then 'string' | |
when 'uniqueidentifier' then 'Guid' | |
when 'datetime' then 'DateTime' | |
when 'bit' then 'bool' | |
else 'string' | |
end | |
if (@pos = 1) | |
print ' [Key]' | |
if (@nullable = 'NO' AND @pos > 1) | |
print ' [Required]' | |
if (@sType = 'string' and @maxLen <> '-1') | |
print ' [StringLength(' + convert(varchar(4),@maxLen) + ')]' | |
if (@sType = 'datetime') | |
print ' [Column(TypeName = "datetime")]' | |
select @sProperty = ' public ' + @sType + @isNullable + ' ' + @columnName + ' { get; set; }' | |
print @sProperty | |
print '' | |
fetch next from column_cursor into @columnName, @nullable, @datatype, @maxlen, @pos, @isNullable | |
end | |
close column_cursor | |
deallocate column_cursor | |
print '}' | |
print '' | |
fetch next from table_cursor into @schema, @tableName | |
end | |
close table_cursor | |
deallocate table_cursor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment