Dapper Class Generator is a small tool for POCOs (Plain Old Class Object) generation using T4 text templates.
Dapper is an object-relational mapping (ORM) for the .NET platform. It provides a framework for mapping an object-oriented domain model to a traditional relational database.
This is an Open Source (MIT) project, you may find source code in my GitHub repository.
Features
- Supports Dapper.Contrib
- Supports FluentValidation
- Very fast - single query collects all data needed for generation
- Easy to change - just edit T4 template & process it
If you are here only for the table details collector script, here you are:
SELECT
SchemaName = SCHEMA_NAME(t.schema_id),
TableName = t.[name],
ColumnName = c.[name],
ColumnPosition = c.column_id,
IsNullable = c.is_nullable,
DataType = ty.[name],
MaximumLength = COLUMNPROPERTY(c.object_id, c.[name], 'CharMaxLen'),
IsIdentity = c.is_identity,
IsComputed = c.is_computed,
IsPrimary = ISNULL(pk.is_primary_key, 0),
RelationshipXML = fk.Relationship
FROM sys.tables t
INNER JOIN sys.columns c ON c.object_id = t.object_id
LEFT JOIN sys.types ty ON ty.user_type_id = c.user_type_id
OUTER APPLY (
SELECT i.is_primary_key
FROM sys.indexes i
INNER JOIN sys.index_columns ic ON ic.object_id = i.object_id
AND ic.index_id = i.index_id
AND ic.column_id = c.column_id
WHERE i.object_id = t.object_id
AND i.is_primary_key = 1
) pk
OUTER APPLY (
SELECT
TableName = t2.[name],
Relationships = (
SELECT
ParentColumnName = pc.[name],
ReferenceColumnName = rc.[name]
FROM sys.foreign_key_columns fkca
INNER JOIN sys.columns pc ON fkca.parent_column_id = pc.column_id
AND fkca.parent_object_id = pc.object_id
INNER JOIN sys.columns rc ON fkca.referenced_column_id = rc.column_id
AND fkca.referenced_object_id = rc.object_id
WHERE fkca.constraint_object_id = fkc.constraint_object_id
AND fkca.parent_object_id = fkc.parent_object_id
FOR XML PATH('Relationship'), TYPE
)
FROM sys.foreign_key_columns fkc
INNER JOIN sys.tables t2 ON t2.object_id = fkc.referenced_object_id
WHERE fkc.parent_column_id = c.column_id
AND fkc.parent_object_id = c.object_id
AND fkc.constraint_column_id = 1
FOR XML PATH('RelationshipRoot')
) fk (Relationship)
ORDER BY SchemaName, TableName, ColumnPosition
Used Technologies
- .NET 5
- CommandLineParser
- Dapper
- T4 Templates
- Humanizer