Example #1
0
        private static Tables GetTables()
        {
            CodeFirstTools.TableRename           = (name, schema) => name;                 // Do nothing by default
            CodeFirstTools.UpdateColumn          = (Column column, Table table) => column; // Do nothing by default
            CodeFirstTools.StoredProcedureRename = (name, schema) => name;                 // Do nothing by default
            Inflector.PluralizationService       = new EnglishPluralizationService(new[]
            {
                // Create custom ("Singular", "Plural") forms for one-off words as needed
                new CustomPluralizationEntry("LiveQuiz", "LiveQuizzes"),
                new CustomPluralizationEntry("Course", "Courses"),
                new CustomPluralizationEntry("CustomerStatus", "CustomerStatus"), // Use same value to prevent pluralisation
                new CustomPluralizationEntry("EmployeeStatus", "EmployeeStatus")
            });
            DbProviderFactory dbf = CodeFirstTools.GetDbProviderFactory();

            return(CodeFirstTools.LoadTables(dbf));
        }
        public override void CreateCode()
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine(CodeContent);
            try
            {
                if (null == ProjectContainer.DomainEntity)
                {
                    throw new Exception("Entity2Code DomainEntity Project Can not be Find");
                }
                string path = Path.Combine(ProjectContainer.DomainEntity.ToDirectory(), _entity.Entity + ".cs");
                if (File.Exists(path) == false)
                {
                    throw new Exception("Entity2Code DomainEntity Project Can not Find ProjectItem " + _entity.Entity + ".cs");
                }
                if (SolutionCommon.infrastryctType == InfrastructType.DbFirst)
                {
                    using (StreamReader reader = new StreamReader(path))
                    {
                        while (reader.Peek() != -1)
                        {
                            string line = reader.ReadLine();
                            if (line.IndexOf("get") != -1 && line.IndexOf("set") != -1 && line.IndexOf("virtual") == -1)
                            {
                                result.AppendLine("        [DataMember]");
                                result.AppendLine(line);
                            }
                        }
                    }
                }
                else
                {
                    Table tb = CodeFirstLogic.GetTables().GetTable(_entity.Entity, CodeFirstTools.SchemaName);
                    foreach (Column clm in tb.Columns)
                    {
                        result.AppendLine("        [DataMember]");
                        result.AppendLine(string.Format("        public {0}{1} {2} {3}", clm.PropertyType, CodeFirstTools.CheckNullable(clm), clm.PropertyName, "{get; set; }"));
                    }
                }
                result.AppendLine("   }");
                result.AppendLine("}");

                string targetPath = ModelPathConverter.GetCodePath(_consType, IsOverWrite, _entity);
                using (FileStream create = new FileStream(targetPath, FileMode.OpenOrCreate))
                {
                    byte[] buffer = Encoding.Default.GetBytes(result.ToString());
                    create.Write(buffer, 0, buffer.Length);
                }
                BuildResult = ProjectContainer.Data2Object.AddFromFile(targetPath);

                base.CreateCode();
            }
            catch (Exception ex)
            {
                MsgBoxHelp.ShowError("创建代码出现异常:", ex);
            }
        }