public string GetRelationConfig()
        {
            //当前类型的所有公共属性
            var allFields = DbModelType.GetProperties();

            //获取简单类型字段
            var simpleTypeFields = allFields.Where(x => !x.PropertyType.IsGenericType && !typeof(IDbModel).IsAssignableFrom(x.PropertyType));

            //导航属性字段,表示关系
            var navigationFields = allFields.Where(x => x.PropertyType.IsGenericType || typeof(IDbModel).IsAssignableFrom(x.PropertyType)).ToList();

            if (simpleTypeFields.Count() != 2 || navigationFields.Count() != 2)
            {
                throw new Exception($"实体{DbModelType}的导航属性配置不正确,在多对多的关系映射中,必须包含2个Model的Id以及对应的导航属性,如果不是多对多的映射,不需要继承自IRelationMap接口,直接遵守微软约定大约配置的法则EFCore即可自动配置.");
            }

            StringBuilder sb = new StringBuilder();

            //复合主键设置
            var mutliKey = $"builder.HasKey(t => new { "{" + string.Join(',', simpleTypeFields.Select(x => "t." + x.Name)) + "}" });";

            sb.AppendLine(mutliKey);

            var leftModelName = navigationFields[0].Name;

            var leftModelNavigations = navigationFields[0].PropertyType.GetProperties().Where(x => x.PropertyType.GenericTypeArguments.Any(n => n == DbModelType));

            if (leftModelNavigations.Count() != 1)
            {
                throw new Exception($"类型{navigationFields[0].PropertyType}中有且只能有一个泛型类型为{DbModelType}的导航属性");
            }

            var leftModelNavigationFieldName = leftModelNavigations.First().Name;

            var rightModelName = navigationFields[1].Name;

            var rightModelNavigations = navigationFields[1].PropertyType.GetProperties().Where(x => x.PropertyType.GenericTypeArguments.Any(n => n == DbModelType));

            if (rightModelNavigations.Count() != 1)
            {
                throw new Exception($"类型{navigationFields[0].PropertyType}中有且只能有一个泛型类型为{DbModelType}的导航属性");
            }

            var rightModelNavigationFieldName = rightModelNavigations.First().Name;

            //配置左边的一对多关系
            var leftConfig = $"builder.HasOne(t => t.{leftModelName}).WithMany(x => x.{leftModelNavigationFieldName}).HasForeignKey(t => t.{leftModelName+"Id"});";

            //配置右边的一对多关系
            var rightConfig = $"builder.HasOne(t => t.{rightModelName}).WithMany(x => x.{rightModelNavigationFieldName}).HasForeignKey(t => t.{rightModelName + "Id"});";

            sb.AppendLine(leftConfig);
            sb.AppendLine(rightConfig);

            return(sb.ToString());
        }
示例#2
0
        private string OtherModel()
        {
            //当前类型的所有公共属性
            var allFields = DbModelType.GetProperties();

            //排除导航属性字段,这些字段是以关系的形式处理
            var simpleTypeFields = allFields.Where(x => !x.PropertyType.IsGenericType && !typeof(IDbModel).IsAssignableFrom(x.PropertyType));

            StringBuilder sb = new StringBuilder();

            simpleTypeFields.ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));

            return(sb.ToString());
        }
示例#3
0
        public string GetTableConfig()
        {
            var queryFilterConfig = "";

            //是否有软删除字段
            var hasIsDelete = DbModelType.GetProperties().Count(x => x.Name == "IsDelete") > 0;

            if (hasIsDelete)
            {
                queryFilterConfig = ".HasQueryFilter(x=> !x.IsDelete)";
            }
            var tabConfig = $"builder.ToTable(\"{DbModelType.Name.UpperCharToUnderLine()}\"){queryFilterConfig};";

            return(tabConfig);
        }
        public string GetFieldsConfig()
        {
            var baseFields = typeof(BaseModel).GetProperties();

            //取到自己的属性字段,不要BaseModel的
            var selfFields = DbModelType.GetProperties().Where(x => !baseFields.Any(dx => { return(dx.Name == x.Name); }));

            //排除导航属性字段,这些字段是以关系的形式
            var simpleTypeFields = selfFields.Where(x => !x.PropertyType.IsGenericType && !typeof(BaseModel).IsAssignableFrom(x.PropertyType));


            StringBuilder sb = new StringBuilder();

            sb.Clear();
            sb.AppendLine(GetFieldConfig(baseFields.Where(x => x.Name == "Id").First()));
            simpleTypeFields.ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));
            baseFields.Where(x => x.Name != "Id").ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));

            return(sb.ToString());
        }
示例#5
0
        private string InheritBaseModel()
        {
            var baseFields = typeof(BaseModel).GetProperties();

            //当前类型的所有公共属性
            var allFields = DbModelType.GetProperties();

            //排除BaseModel的属性
            var selfFields = allFields.Where(x => !baseFields.Any(dx => { return(dx.Name == x.Name); }));

            //排除导航属性字段,这些字段是以关系的形式处理
            var simpleTypeFields = selfFields.Where(x => !x.PropertyType.IsGenericType && !typeof(IDbModel).IsAssignableFrom(x.PropertyType));


            StringBuilder sb = new StringBuilder();

            sb.Clear();
            sb.AppendLine(GetFieldConfig(baseFields.Where(x => x.Name == "Id").First()));
            simpleTypeFields.ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));
            //排除在子类中被private的字段
            baseFields.Where(x => x.Name != "Id" && allFields.Any(n => { return(n.Name == x.Name); })).ToList().ForEach(x => sb.AppendLine(GetFieldConfig(x)));

            return(sb.ToString());
        }