public static CSharpClassDefinition GetRequestModelExtensionsClassDefinition(this AspNetCoreProject project)
        {
            var classDefinition = new CSharpClassDefinition
            {
                Namespaces = new List <string>
                {
                    "System",
                    project.GetEntityLayerNamespace()
                },
                Namespace = project.GetRequestModelsNamespace(),
                Name      = "Extensions",
                IsStatic  = true
            };

            foreach (var table in project.Database.Tables)
            {
                if (!table.HasDefaultSchema())
                {
                    classDefinition.Namespaces.AddUnique(project.GetEntityLayerNamespace(table.Schema));
                }

                classDefinition.Methods.Add(GetToEntityMethod(project, table));
                classDefinition.Methods.Add(GetToRequestModelMethod(project, table));
            }

            return(classDefinition);
        }
示例#2
0
        public static CSharpClassDefinition GetResponsesExtensionsClassDefinition(this AspNetCoreProject project, ITable table)
        {
            var classDefinition = new CSharpClassDefinition
            {
                Namespaces = new List <string>
                {
                    "System",
                    "System.ComponentModel.DataAnnotations",
                    table.HasDefaultSchema() ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema),
                    table.HasDefaultSchema() ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema)
                },
                Namespace = project.GetRequestModelsNamespace(),
                Name      = table.GetRequestModelName()
            };

            var selection = project.GetSelection(table);

            foreach (var column in table.Columns.Where(item => selection.Settings.ConcurrencyToken != item.Name).ToList())
            {
                var property = new PropertyDefinition(EfCore.DatabaseExtensions.ResolveType(project.Database, column), column.GetPropertyName());

                if (table.PrimaryKey?.Key.Count > 0 && table.PrimaryKey?.Key.First() == column.Name)
                {
                    property.Attributes.Add(new MetadataAttribute("Key"));
                }

                if (!column.Nullable && table.PrimaryKey?.Key.Count > 0 && table.PrimaryKey?.Key.First() != column.Name)
                {
                    property.Attributes.Add(new MetadataAttribute("Required"));
                }

                if (project.Database.ColumnIsString(column) && column.Length > 0)
                {
                    property.Attributes.Add(new MetadataAttribute("StringLength", column.Length.ToString()));
                }

                classDefinition.Properties.Add(property);
            }

            return(classDefinition);
        }