ITXP365(智码IT网)

分享电脑知识,传播IT技巧

致敬爱学习的您,祝您访问愉快!

orm框架SqlSugar入门4 自定义类文件模板,如自定义类文件、构造函数、命名空间、属性描述等


发布:智码IT网   阅读:
        //【自定义类文件模板】
        private void button6_Click(object sender, EventArgs e)
        {
            using (SqlSugarClient db = new SqlSugarClient(MyConnConfig.connectionConfig))
            {
                db.DbFirst
                    //设置类文件模板
                    .SettingClassTemplate(old => { return "//这里的内容出现在类文件顶部\r\n" + old + "//这里的内容将出现的类文件的结尾"; }) //在整个文件末尾加上:

                    //设置构造函数模板
                    .SettingConstructorTemplate(old => { return old; })

                    //设置命名空间模板
                    .SettingNamespaceTemplate(old => { return old + "\r\nusing SqlSugar; //这行是代码生成的,追加到using列表的末尾处"; })

                    //设置属性描述模板(即xml注释模板)
                    .SettingPropertyDescriptionTemplate(old => { return old + "这里的内容出现在xml注释结尾处"; })
                    /*   执行以上代码后,在生成的属性注释后面加上【这里的内容出现在xml注释结尾处】,示例结果如下:

                        /// <summary>
                        /// Desc:
                        /// Default:
                        /// Nullable:False
                        /// </summary>这里的内容出现在xml注释结尾处   
                        public int id {get;set;}

                    */

                    //设置属性模板
                    .SettingPropertyTemplate((columns, temp, type) =>
                    {
                        List<string> attributeList = new List<string>();
                        if (columns.IsPrimarykey)
                        {
                            attributeList.Add("IsPrimaryKey=true");
                        }
                        if (columns.IsIdentity)
                        {
                            attributeList.Add("IsIdentity=true");
                        }

                        var columnAttributesStr = "\r\n       [SugarColumn({0})]";
                        if (attributeList.Count == 0)
                        {
                            columnAttributesStr = "";
                        }

                        return temp
                            .Replace("{PropertyType}", type)
                            .Replace("{PropertyName}", columns.DbColumnName)
                            .Replace("{SugarColumn}", string.Format(columnAttributesStr, string.Join(",", attributeList)));
                    })
                   /*
                        执行以上代码后,会生成类似如下的代码:
                        [SuggarColumn(IsPrimaryKey=true,IsIdentity=true)]
                        public int ID {get;set;}
                    */

                   .CreateClassFile(dirPath);
            }
        }


最终生成的类文件示例代码如下:

//这里的内容出现在类文件顶部
using System;
using System.Linq;
using System.Text;

using SqlSugar; //这行是代码生成的,追加到using列表的末尾处
namespace Models
{
    ///<summary>
    ///学生表
    ///</summary>
    public partial class Student
    {
           public Student(){


           }
           /// <summary>
           /// Desc:主键ID
           /// Default:
           /// Nullable:False
           /// </summary>这里的内容出现在xml注释结尾处           
       [SugarColumn(IsPrimaryKey=true,IsIdentity=true)]
           public int ID {get;set;}

           /// <summary>
           /// Desc:学生姓名
           /// Default:
           /// Nullable:True
           /// </summary>这里的内容出现在xml注释结尾处           
           public string Name {get;set;}

           /// <summary>
           /// Desc:年龄
           /// Default:18
           /// Nullable:True
           /// </summary>这里的内容出现在xml注释结尾处           
           public int? Age {get;set;}

    }
}
//这里的内容将出现的类文件的结尾

上一篇:没有了
下一篇:没有了

© 2001-智码IT网 www.itxp365.com版权所有

蜀ICP备2021001527号