Skip to main content

Posts

Showing posts from April, 2011

Updated Code To Get Class Definition From Table Structure

My previous post on Script to Create MetadataType classes was to work with LINQ-To-SQL and partial classes. EF 4.1 introduces Code First which uses POCOs and here is the code to create a class definition from a table’s structure.Enjoy.SET NOCOUNT ON declare @TableName varchar(256) = 'BusinessTypes' declare @EntityName varchar(256) = 'BusinessType' declare @TableSchema varchar(256) = 'dbo' declare @ColumnName varchar(256) , @DataType varchar(256) , @NewDataType varchar(256) , @MaxLength int , @Nullable varchar(5) declare @Lines table (Line varchar(1000)) insert into @Lines select 'public class ' + @EntityName insert into @Lines select '{' declare @DataTypes table (SqlDataType varchar(1000), DataType varchar(1000)) insert into @DataTypes (SqlDataType, DataType) values ('bit', 'bool') insert into @DataTypes (SqlDataType, DataType) values ('char', 'string') insert into @DataTypes (SqlDataType,…