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);
            }
        }
Example #2
0
        public static void CreateCodeFirst(bool overWrite = true)
        {
            if (ProjectContainer.Infrastructure == null)
            {
                ProjectContainer.Infrastructure = SolutionCommon.Dte.AddClassLibrary(SolutionCommon.Infrastructure, true);
            }
            CodeStaticManager codeManager = new CodeStaticManager(ConstructType.DbContextExtensions);

            codeManager.BuildTaget = new StringCodeArgment()
            {
                Folder = "Extention", Name = "DbContextExtensions.cs", Target = ProjectContainer.Infrastructure
            };
            codeManager.CreateCode();

            CodeFirstLogic.WriteConfigFies(overWrite);

            Tables tables = CodeFirstLogic.GetTables();

            CodeBuilderContainer.DBContextBuilder.Clear();
            foreach (Table tb in tables)
            {
                CodeAppendManager appendManager = new CodeAppendManager(ConstructType.DBContext, new TemplateEntity()
                {
                    Entity = tb.Name, Data2Obj = tb.NameHumanCase
                });
                appendManager.BuildTaget = CodeBuilderContainer.DBContextBuilder;
                appendManager.CreateCode();
            }
            ModelContainer.Regist("$ContextName$", CodeFirstTools.DbContextName, "上下文名称");
            codeManager = new CodeStaticManager(ConstructType.DBContext);

            codeManager.BuildTaget = new StringCodeArgment()
            {
                Name = CodeFirstTools.DbContextName + ".cs", Target = ProjectContainer.Infrastructure
            };
            codeManager.CreateCode();

            codeManager            = new CodeStaticManager(ConstructType.ContextUnit);
            codeManager.BuildTaget = new StringCodeArgment()
            {
                Name = CodeFirstTools.DbContextName + "Unit.cs", Encode = Encoding.Default, Target = ProjectContainer.Infrastructure
            };
            codeManager.CreateCode();
        }
Example #3
0
 /// <summary>
 /// 获取所有领域层实体信息
 /// </summary>
 /// <param name="overRead">是否重新读</param>
 /// <returns></returns>
 public static List <TemplateEntity> GetEntitys(bool overRead)
 {
     if (_entitys == null || overRead)
     {
         _entitys = new List <TemplateEntity>();
         if (SolutionCommon.infrastryctType == InfrastructType.DbFirst)
         {
             if (ProjectContainer.DomainEntity == null)
             {
                 throw new Exception("Entity2Code DomainEntity Project Can not be Find");
             }
             string   entityDir = ProjectContainer.DomainEntity.ToDirectory();
             string[] files     = Directory.GetFiles(entityDir, "*.cs");
             if (null == files || files.Length == 0)
             {
                 throw new Exception("Entity2Code DomainEntity ProjectItem is Null");
             }
             foreach (string file in files)
             {
                 string entity = Path.GetFileNameWithoutExtension(file);
                 _entitys.Add(new TemplateEntity()
                 {
                     Entity = entity, Data2Obj = ModelNameConverter.GetData2Obj(entity)
                 });
             }
         }
         else
         {
             foreach (Table item in CodeFirstLogic.GetTables())
             {
                 _entitys.Add(new TemplateEntity()
                 {
                     Entity = item.Name, Data2Obj = item.NameHumanCase
                 });
             }
         }
     }
     return(_entitys);
 }