Beispiel #1
0
        public IEnumerable <PropInfo> GetPropInfos(Type type)
        {
            var instance = type.Assembly.CreateInstance(type.FullName);

            foreach (var item in type.GetProperties())
            {
                if (item.Name == "Id")
                {
                    continue;
                }

                if (item.GetCustomAttribute <ExcludeAttribute>() != null)
                {
                    continue;
                }

                var summary = T4Help.GetPropertySummary(item, XmlDocDir);

                var defaultValue = instance.GetValue(item.Name)?.ToString();
                if (defaultValue == null)
                {
                    defaultValue = "null";
                }
                if (T4Help.GetNullableType(item.PropertyType) == typeof(string))
                {
                    defaultValue = $"\"{defaultValue}\"";
                }
                if (T4Help.GetNullableType(item.PropertyType) == typeof(string))
                {
                    defaultValue = $"\"{defaultValue}\"";
                }
                yield return(new PropInfo()
                {
                    Summary = summary,
                    AttrInfos = GetAttrInfos(item),
                    TypeName = T4Help.GetTypeName(item.PropertyType),
                    Name = item.Name,
                    DefaultValue = defaultValue
                });

                if (TryGetAttribute <RelatedToAttribute>(item, out var relatedToAttribute))
                {
                    var relateTypeName = relatedToAttribute.RelatedType.Name;
                    yield return(new PropInfo()
                    {
                        Summary = summary,
                        TypeName = $"{relateTypeName}ViewModel",
                        Name = item.Name.Replace("_Id", "")
                    });
                }
            }

            if (typeof(ITreeEntity).IsAssignableFrom(type))
            {
                yield return new PropInfo
                       {
                           Name         = "HasTreeChildren",
                           DefaultValue = "false",
                           Summary      = "是否有下级",
                           TypeName     = "bool"
                       }
            }
            ;

            if (typeof(IHaveMultiFile).IsAssignableFrom(type))
            {
                yield return new PropInfo
                       {
                           Name         = "Files",
                           DefaultValue = "Array.Empty<ResourceModel>()",
                           Summary      = "附件",
                           TypeName     = "IEnumerable<ResourceModel>"
                       }
            }
            ;
        }
Beispiel #2
0
        public virtual void ModelEntityCreating(EntityTypeBuilder <T> entityTypeBuilder)
        {
            //Debugger.Launch();

            var entityType    = typeof(T);
            var currNameSpace = string.Join(",", entityType.Namespace.Split(new char[] { '.' }).Skip(2));

            if (typeof(IEntity).IsAssignableFrom(entityType))
            {
                /*指定主键*/
                entityTypeBuilder.HasKey("Id");
                entityTypeBuilder.Property("Id").ValueGeneratedNever();
                entityTypeBuilder.ToTable($"{currNameSpace}_{entityType.Name}".ToLower());
                entityTypeBuilder.HasComment(T4Help.GetClassSummary(entityType, AppDomain.CurrentDomain.BaseDirectory));
            }
            else
            {
                entityTypeBuilder
                .HasNoKey()
                .ToView($"{currNameSpace}_{entityType.Name}".ToLower());
            }


            /*过滤掉软删除的*/
            if (typeof(IHasSoftDelete).IsAssignableFrom(entityType))
            {
                entityTypeBuilder.Property <bool>("isdeleted");
                entityTypeBuilder.HasQueryFilter(v => !EF.Property <bool>(v, "isdeleted"));
                entityTypeBuilder.HasIndex("isdeleted").HasDatabaseName($"Index_{entityType.Name}_isdeleted");
            }

            if (typeof(IHasTenant).IsAssignableFrom(entityType))
            {
                entityTypeBuilder.Property <string>("tenant_id").HasMaxLength(25);
                entityTypeBuilder.HasIndex("tenant_id").HasDatabaseName($"Index_{entityType.Name}_tenant_id");
            }

            foreach (var item in typeof(T).GetProperties())
            {
                if (item.GetCustomAttribute <NotMappedAttribute>() != null)
                {
                    continue;
                }

                var propType = T4Help.GetNullableType(item.PropertyType);

                var propertyBuilder = entityTypeBuilder
                                      .Property(item.Name)
                                      .HasColumnName(item.Name.ToLower());

                propertyBuilder.HasComment(T4Help.GetPropertySummary(item, AppDomain.CurrentDomain.BaseDirectory));

                /*索引ID*/
                if (typeof(IEntity).IsAssignableFrom(entityType))
                {
                    if (item.Name.EndsWith("Id") && item.Name != "Id")
                    {
                        entityTypeBuilder.HasIndex(item.Name).HasDatabaseName($"Index_{entityType.Name}_{item.Name}");
                    }

                    if (propType == typeof(string))
                    {
                        /*所有字符串,指定为unicode*/
                        propertyBuilder.IsUnicode();

                        /*所有ID,指定长度为25*/
                        if (item.Name.EndsWith("Id"))
                        {
                            propertyBuilder.HasMaxLength(25);
                        }
                    }
                }
                if (propType == typeof(decimal))
                {
                    propertyBuilder.HasPrecision(10, 2);
                }

                else if (propType.IsEnum)
                {
                    propertyBuilder.HasConversion <string>().HasMaxLength(50);
                }
            }
        }