Skip to content

Instantly share code, notes, and snippets.

@renant
Created March 11, 2019 17:21
Show Gist options
  • Save renant/907a59dd8a089aa9a8b8cc034796b6ce to your computer and use it in GitHub Desktop.
Save renant/907a59dd8a089aa9a8b8cc034796b6ce to your computer and use it in GitHub Desktop.
Gerador de Map do entity a partir de uma tabela
declare @schema varchar(100) = 'schema'
declare @tableName varchar(100) = 'tableName'
declare @objectId bigint;
select @objectId = t.object_id
from sys.tables t
inner join sys.schemas s on s.schema_id = t.schema_id
where s.name = @schema and t.name = @tableName
select concat(' public class ', @tableName, 'MapSqlServer : IEntityTypeConfiguration<',@tableName,'>')
union all
select ' {'
union all
select concat(' public void Configure(EntityTypeBuilder<',@tableName, '> builder)')
union all
select ' {'
union all
select concat(' builder.ToTable("',@tableName,'", "',@schema,'");')
union all
select ' builder.HasKey(x => x.Id);'
union all
select concat(' builder.Property(x => x.',c.name,').HasColumnName("',c.name,'");')
from sys.columns c
where object_id = @objectId
union all
select ' builder.Property(p => p.Id).UseSqlServerIdentityColumn();'
union all
select ' }'
union all
select ' }'
select @objectId;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment