public override BUSBusinessComponent DataToBusiness(BusinessComponent dataEntity, TContext context)
        {
            BUSBusinessComponent businessEntity = base.DataToBusiness(dataEntity, context);
            Table table = context.Tables.AsNoTracking().FirstOrDefault(i => i.Id == dataEntity.TableId);

            if (table != null)
            {
                businessEntity.Table     = table;
                businessEntity.TableId   = table.Id;
                businessEntity.TableName = table.Name;
            }
            businessEntity.ShadowCopy = dataEntity.ShadowCopy;
            return(businessEntity);
        }
Exemple #2
0
        private static IEnumerable <PropertyDeclarationSyntax> CreateTableColumns(IEnumerable <TableColumn> tableColumns, MainContext context)
        {
            List <PropertyDeclarationSyntax> properties = new List <PropertyDeclarationSyntax>();

            foreach (TableColumn tableColumn in tableColumns)
            {
                if (!IsSystemProperty(tableColumn.Name))
                {
                    PropertyDeclarationSyntax autoProp = GenerateAutoProp(tableColumn.Type, tableColumn.Name, tableColumn.IsNullable, new SyntaxKind[] { PublicKeyword });

                    // Составление внешнего ключа
                    if (tableColumn.IsForeignKey)
                    {
                        Table foreignTable = context.Tables.AsNoTracking().FirstOrDefault(i => i.Id == tableColumn.ForeignTableId);
                        properties.Add(GetNavProperty(foreignTable, tableColumn));
                        UpdateForeignProperty(foreignTable, tableColumn);
                    }
                    properties.Add(autoProp);
                }
            }
            return(properties);
        }
        public ActionResult <object> ApplyTable()
        {
            Table table = context.Tables
                          .AsNoTracking()
                          .Include(tc => tc.TableColumns)
                          .FirstOrDefault(i => i.Id.ToString() == GetSelectedRecord("Table"));

            if (table != null && table.TableColumns.Count != 0 && (table.TableColumns.FirstOrDefault(u => u.NeedCreate == true) != null || table.TableColumns.FirstOrDefault(u => u.NeedUpdate == true) != null))
            {
                string migrationName = table.Name + "_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "_" + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
                CodGenMigration.CreateMigration(context, table, migrationName);
                table.IsApply = true;
                context.Entry(table).State = EntityState.Modified;
                table.TableColumns.ForEach(tableColumn =>
                {
                    tableColumn.NeedCreate           = false;
                    tableColumn.NeedUpdate           = false;
                    context.Entry(tableColumn).State = EntityState.Modified;
                });
                context.SaveChanges();
            }
            return(Ok());
        }
Exemple #4
0
        public static void GenerateModel(MainContext context, Type entityType, Guid recordId)
        {
            SyntaxTree syntaxTree;
            string     entityName = string.Empty;
            IEnumerable <MemberDeclarationSyntax> members = new List <MemberDeclarationSyntax>();

            switch (entityType.Name)
            {
            case "Table":
                Table table = context.Tables
                              .AsNoTracking()
                              .Include(tc => tc.TableColumns)
                              .FirstOrDefault(i => i.Id == recordId);
                entityName = table.Name;
                members    = CreateTableColumns(table.TableColumns, context);
                break;

            case "BusinessComponent":
                BusinessComponent busComp = context.BusinessComponents
                                            .AsNoTracking()
                                            .Include(f => f.Fields)
                                            .ThenInclude(tc => tc.TableColumn)
                                            .Include(f => f.Fields)
                                            .ThenInclude(tc => tc.JoinColumn)
                                            .FirstOrDefault(i => i.Id == recordId);
                entityName = busComp.Name;
                members    = CreateFields(busComp.Fields);
                break;

            case "Applet":
                Applet applet = context.Applets
                                .AsNoTracking()
                                .Include(c => c.Controls)
                                .ThenInclude(f => f.Field)
                                .Include(c => c.Columns)
                                .ThenInclude(f => f.Field)
                                .FirstOrDefault(i => i.Id == recordId);
                entityName = applet.Name;
                members    = CreateColumns(applet.Columns, context).Concat(CreateControls(applet.Controls, context));
                break;
            }
            string permissibleName = GetPermissibleName(entityName);

            if (TryGetModel(GSCrmApplication, permissibleName, entityType.Name, out syntaxTree))
            {
                SyntaxNode             rootNode  = syntaxTree.GetRoot();
                ClassDeclarationSyntax classNode = rootNode
                                                   .DescendantNodes()
                                                   .OfType <ClassDeclarationSyntax>()
                                                   .SingleOrDefault(n => n.Identifier.ValueText == permissibleName);
                List <PropertyDeclarationSyntax> interProps = new List <PropertyDeclarationSyntax>();
                classNode.Members.OfType <PropertyDeclarationSyntax>().ToList().ForEach(property =>
                {
                    if (!members.Contains(property) && !property.Identifier.ValueText.StartsWith(SystemPrefix))
                    {
                        interProps.Add(property);
                    }
                });
                rootNode   = rootNode.ReplaceNode(classNode, classNode.WithMembers(List(members.Except(interProps))));
                syntaxTree = SyntaxTree(rootNode).WithFilePath(syntaxTree.FilePath);
            }
            else
            {
                syntaxTree = CreateModel(permissibleName, entityType, members);
                if (entityType == typeof(Table))
                {
                    Compile("GSCrmApplicationContext", syntaxTree);
                }
            }
            WriteTree(GSCrmApplication, permissibleName, entityType.Name, syntaxTree);
            Compile("GSCrm" + entityType.Name, syntaxTree);
        }
        public override void OnRecordCreate(BusinessComponent recordToCreate, DbSet <BusinessComponent> entities, TContext context)
        {
            if (recordToCreate.Table != null)
            {
                // Table
                Table table = context.Tables
                              .AsNoTracking()
                              .Select(t => new
                {
                    id           = t.Id,
                    tableColumns = t.TableColumns.Select(tableColumn => new
                    {
                        id   = tableColumn.Id,
                        name = tableColumn.Name
                    })
                })
                              .Select(t => new Table
                {
                    Id           = t.id,
                    TableColumns = t.tableColumns.Select(tableColumn => new TableColumn
                    {
                        Id   = tableColumn.id,
                        Name = tableColumn.name
                    }).ToList()
                })
                              .FirstOrDefault(i => i.Id == recordToCreate.TableId);

                // Table columns
                IEnumerable <TableColumn> tableColumns = table.TableColumns;

                // Add fields
                context.Fields.AddRange(new List <Field>()
                {
                    new Field()
                    {
                        Name          = "Id",
                        BusComp       = recordToCreate,
                        BusCompId     = recordToCreate.Id,
                        TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "Id").Id
                    },
                    new Field()
                    {
                        Name          = "Created",
                        BusComp       = recordToCreate,
                        BusCompId     = recordToCreate.Id,
                        TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "Created").Id
                    },
                    new Field()
                    {
                        Name          = "CreatedBy",
                        BusComp       = recordToCreate,
                        BusCompId     = recordToCreate.Id,
                        TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "CreatedBy").Id
                    },
                    new Field()
                    {
                        Name          = "LastUpdated",
                        BusComp       = recordToCreate,
                        BusCompId     = recordToCreate.Id,
                        TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "LastUpdated").Id
                    },
                    new Field()
                    {
                        Name          = "UpdatedBy",
                        BusComp       = recordToCreate,
                        BusCompId     = recordToCreate.Id,
                        TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "UpdatedBy").Id
                    }
                });
                context.Entry(recordToCreate.Table).State = EntityState.Unchanged;
            }
            base.OnRecordCreate(recordToCreate, entities, context);
        }