예제 #1
0
        private static async Task GenerateModels(IPowerService _api)
        {
            string currentFolder = AppDomain.CurrentDomain.BaseDirectory;
            var    tables        = await _api.GetTablesAsync();

            using (StreamWriter sw = File.CreateText(currentFolder + "\\MPModels.cs"))
            {
                sw.WriteLine("using System;");
                sw.WriteLine("using System.Collections.Generic;");
                sw.WriteLine("using System.ComponentModel.DataAnnotations;");
                sw.WriteLine("using System.ComponentModel.DataAnnotations.Schema;");
                sw.WriteLine("using System.Linq;");
                sw.WriteLine("using Newtonsoft.Json;");
                sw.WriteLine("using System.Web.DynamicData;");
                sw.WriteLine("");
                sw.WriteLine("namespace MinistryPlatform.Models");
                sw.WriteLine("{");

                // Produce Base Class (Optional)
                //sw.WriteLine("\tpublic class mpBaseClass");
                //sw.WriteLine("\t{");
                //sw.WriteLine("\t}");

                foreach (var item in tables)
                {
                    // Ignore all Tables starting with _ character
                    if (item.Name.StartsWith("_"))
                    {
                        continue;
                    }

                    // Ignore SQL Views
                    if (item.Name.StartsWith("mp_vw"))
                    {
                        continue;
                    }

                    string modelName = item.Name.Replace("_", "");

                    modelName = fixModelName(modelName);

                    modelName += "Model";
                    //sw.WriteLine("[Table(\"" + item.Name + "\")]");
                    sw.WriteLine($"\tpublic class {modelName}");
                    sw.WriteLine("\t{");
                    foreach (var c in item.Columns)
                    {
                        //Ignore any field called tablename // Reserved Constant for Model
                        if (c.Name.ToLower() == "tablename")
                        {
                            continue;
                        }

                        WriteColumn(c, sw);
                    }
                    sw.WriteLine("");
                    sw.WriteLine($"\t\tpublic const string TableName = \"{item.Name}\";");
                    sw.WriteLine("\t}");
                    sw.WriteLine("");
                    sw.WriteLine("");
                }
                sw.WriteLine("}");
            }

            return;
        }