示例#1
0
        private string RenderRefProperties(Table table)
        {
            var code = new StringBuilder();

            foreach (var column in table.Columns)
            {
                if (column.IsForeignKey)
                {
                    //如果属性名以 Id 结尾,则直接去除 Id 即可。
                    //否则,映射失败,直接以列名作为引用属性的名称,添加列名 + Id 与属性名的映射。
                    string propertyName = string.Empty;
                    var    columnName   = column.Name;
                    if (columnName.ToLower().EndsWith("id"))
                    {
                        propertyName = columnName.Substring(0, columnName.Length - 2);
                    }
                    else
                    {
                        propertyName = columnName;
                    }

                    //处理一些类似于 RefEntity_Id 的字段,去掉最后的下划线。
                    while (propertyName[0] == '_')
                    {
                        propertyName = propertyName.Substring(1);
                    }
                    while (propertyName[propertyName.Length - 1] == '_')
                    {
                        propertyName = propertyName.Substring(0, propertyName.Length - 1);
                    }

                    //如果字段与属性名不同,需要添加属性的配置。
                    var idProperty = propertyName + "Id";
                    if (!idProperty.EqualsIgnoreCase(columnName))
                    {
                        _tablePropertiesConfig.Add(new PropertyConfig
                        {
                            PropertyName = idProperty,
                            ColumnName   = columnName
                        });
                    }

                    var refEntity    = ToEntityName(column.ForeignConstraint.PKTable);
                    var propertyCode = ItemCodeTemplate.GetRefPropertyCode(
                        ToEntityName(table), refEntity, propertyName,
                        isRequired: column.IsRequired
                        );
                    code.Append(propertyCode);
                }
            }

            return(code.ToString());
        }