Exemplo n.º 1
0
        public EntityContext <TEntity, TProperty> GetAndCreate(SchemaSelector databaseSchema)
        {
            EntityContext <TEntity, TProperty> entityContext = new EntityContext <TEntity, TProperty>
            {
                DatabaseName = databaseSchema.Database.Name
            };

            string dataContextName = StringUtil.ToPascalCase(entityContext.DatabaseName) + "Context";

            dataContextName = _content.UniqueNamer.UniqueClassName(dataContextName);

            entityContext.ClassName = dataContextName;

            var tables = databaseSchema.Tables
                         .Selected()
                         .OrderBy(t => t.SortName)
                         .ToTables();

            _entityTemplate.EntityContext = entityContext;

            _viewEntityTemplate = new ViewEntityTemplate <TEntity, TProperty>(_content)
            {
                EntityContext = entityContext
            };

            //先进行装载实体
            foreach (TableSchema t in tables)
            {
                if (_content.IsManyToMany(t))
                {
                    Debug.WriteLine("多对多表: " + t.FullName);
                    CreateManyToMany(t);
                }
                else
                {
                    Debug.WriteLine("表: " + t.FullName);
                    _entityTemplate.GetAndCreate(t);
                }

                OnSchemaItemProcessed(t.FullName);
            }

            RelationshipTemplate <TEntity, TProperty> relationshipTemplate = new RelationshipTemplate <TEntity, TProperty>(_content)
            {
                EntityContext = entityContext
            };

            MethodTemplate <TEntity, TProperty> methodTemplate = new MethodTemplate <TEntity, TProperty>();

            foreach (TEntity entity in entityContext.Entities)
            {
                TableSchema tableSchema = databaseSchema.Tables.FirstOrDefault(t => t.FullName == entity.FullName);
                if (tableSchema == null)
                {
                    continue;
                }

                relationshipTemplate.ForeignEntity = entity;
                foreach (TableKeySchema tableKey in tableSchema.ForeignKeys.Selected())
                {
                    relationshipTemplate.GetAndCreate(tableKey);
                }

                methodTemplate.Entity = entity;
                methodTemplate.GetAndCreate(tableSchema);
            }

            if (!_content.Settings.IncludeViews)
            {
                return(entityContext);
            }
            // 数据库视图生成EF实体
            var views = databaseSchema.Views
                        .Selected()
                        .OrderBy(t => t.SortName)
                        .ToViews();

            foreach (ViewSchema v in views)
            {
                Debug.WriteLine("视图: " + v.FullName);
                _viewEntityTemplate.GetAndCreate(v);
                OnSchemaItemProcessed(v.FullName);
            }

            return(entityContext);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建“编辑”文件与“生成”文件
        /// </summary>
        /// <typeparam name="TGenerate"></typeparam>
        /// <typeparam name="TEditable"></typeparam>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="entityContext"></param>
        /// <param name="codeTemplate"></param>
        /// <param name="getGeneratedNamegFun"></param>
        /// <param name="getEditableNameFun"></param>
        /// <param name="rootDirectory"></param>
        public static void CreateMultipleEntityClass <TGenerate, TEditable, TEntity, TProperty>(EntityContext <TEntity, TProperty> entityContext, CodeTemplate codeTemplate, Func <TEntity, string> getGeneratedNamegFun, Func <TEntity, string> getEditableNameFun, string rootDirectory)
            where TGenerate : CodeTemplate, new()
            where TEditable : CodeTemplate, new() where TProperty : Property where TEntity : Entity <TProperty>
        {
            TGenerate generatedClass = codeTemplate.Create <TGenerate>();

            codeTemplate.CopyPropertiesTo(generatedClass);

            TEditable editableClass = codeTemplate.Create <TEditable>();

            codeTemplate.CopyPropertiesTo(editableClass);

            //创建根目录
            rootDirectory = Path.GetFullPath(rootDirectory);
            if (!Directory.Exists(rootDirectory))
            {
                Directory.CreateDirectory(rootDirectory);
            }

            foreach (TEntity entity in entityContext.Entities)
            {
                string editableFile = Path.Combine(rootDirectory, getEditableNameFun(entity));
                if (!File.Exists(editableFile))
                {
                    editableClass.SetProperty("Entity", entity);
                    codeTemplate.Response.WriteLine(editableFile);
                    editableClass.RenderToFile(editableFile, true);
                }

                string generatedFile = Path.Combine(rootDirectory, getGeneratedNamegFun(entity));
                generatedClass.SetProperty("Entity", entity);
                codeTemplate.Response.WriteLine(generatedFile);
                generatedClass.RenderToFile(generatedFile, editableFile, true);
            }
        }