void Main()
{
var dataContextName = "ImageBoard";
var schemaName = "user_image";
var outputFolder = "c:\\data\\";
GenerateClasses(dataContextName, schemaName, outputFolder);
}
public void GenerateClasses(string dataContextName, string schemaName, string outputFolder)
{
foreach (var table in this.Mapping.GetTables().Where(t =>
!t.TableName.Contains("sysdiagrams"))
.OrderBy(t => t.TableName))
{
var tableName = Regex
.Replace(table.TableName, @"([\[\]])", string.Empty)
.Replace(schemaName + ".", string.Empty);
var contents = new List<string>();
var columns = from c in table.RowType.DataMembers
where c.DbType != null
select new {
c.Name,
c.DbType,
cType = c.Type.Name,
c.CanBeNull,
c.Type,
c.MappedName,
c.IsPrimaryKey };
StringBuilder sb = new StringBuilder();
sb.AppendLine($"[MetadataType(typeof({tableName}Metadata))]");
sb.AppendLine($"public partial class {tableName} {{");
sb.AppendLine($"\tpublic static bool Add(ref {tableName} {tableName.ToLower()}) {{");
sb.AppendLine($"\t\tvar db = new {dataContextName}DataContext();");
sb.AppendLine($"\t\tvar result = false;\n");
sb.AppendLine($"\t\ttry {{");
sb.AppendLine($"\t\t\tresult = true;");
sb.AppendLine($"\t\t}}");
sb.AppendLine($"\t\tcatch(Exception ex) {{");
sb.AppendLine($"\t\t\tresult = false;");
sb.AppendLine($"\t\t}}\n");
sb.AppendLine($"\t\treturn result;");
sb.AppendLine($"\t}}\n");
sb.AppendLine($"\tpublic static bool Delete({tableName} {tableName.ToLower()}) {{");
sb.AppendLine($"\t\tvar db = new {dataContextName}DataContext();");
sb.AppendLine($"\t\tvar result = false;\n");
sb.AppendLine($"\t\ttry {{");
sb.AppendLine($"\t\t\tresult = true;");
sb.AppendLine($"\t\t}}");
sb.AppendLine($"\t\tcatch(Exception ex) {{");
sb.AppendLine($"\t\t\tresult = false;");
sb.AppendLine($"\t\t}}\n");
sb.AppendLine($"\t\treturn result;");
sb.AppendLine($"\t}}\n");
sb.AppendLine($"\tpublic static bool Update({tableName} {tableName.ToLower()}) {{");
sb.AppendLine($"\t\tvar db = new {dataContextName}DataContext();");
sb.AppendLine($"\t\tvar result = false;\n");
sb.AppendLine($"\t\ttry {{");
sb.AppendLine($"\t\t\tresult = true;");
sb.AppendLine($"\t\t}}");
sb.AppendLine($"\t\tcatch(Exception ex) {{");
sb.AppendLine($"\t\t\tresult = false;");
sb.AppendLine($"\t\t}}\n");
sb.AppendLine($"\t\treturn result;");
sb.AppendLine($"\t}}\n");
sb.AppendLine($"\tpublic static bool Get(int {tableName.ToLower()}ID, out {tableName} {tableName.ToLower()}) {{");
sb.AppendLine($"\t\tvar db = new {dataContextName}DataContext();");
sb.AppendLine($"\t\tvar result = false;\n");
sb.AppendLine($"\t\ttry {{");
sb.AppendLine($"\t\t\tresult = true;");
sb.AppendLine($"\t\t}}");
sb.AppendLine($"\t\tcatch(Exception ex) {{");
sb.AppendLine($"\t\t\tresult = false;");
sb.AppendLine($"\t\t}}\n");
sb.AppendLine($"\t\treturn result;");
sb.AppendLine($"\t}}\n");
sb.AppendLine($"\tprivate sealed class {tableName}Metadata {{");
foreach (var column in columns.Where(c => !c.IsPrimaryKey))
{
sb.AppendLine($"\t\t[Display(Name = \"{column.Name}\", Prompt = \"{column.Name}\")]");
if (!column.CanBeNull)
{
sb.AppendLine($"\t\t[Required(ErrorMessage = \"Required\")]");
}
sb.Append($"\t\tpublic {ConvertType(column.cType)} {column.Name} {{ get; set; }}\n");
}
sb.AppendLine($"\t}}");
sb.AppendLine($"}}\n");
var classCode = sb.ToString();
File.WriteAllText($"{outputFolder}{tableName}.cs", sb.ToString());
}
}
public string ConvertType(string sqlTypeName)
{
switch (sqlTypeName)
{
case "DateTime":
return "DateTime";
case "Int16":
case "Int32":
return "int";
case "Boolean":
case "Bit":
return "bool";
case "String":
return "string";
default:
return sqlTypeName;
}
}
Comments