Skip to content
sunkaixuan edited this page May 6, 2019 · 11 revisions

1.Create SqlSugarClient

SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = DbType.SqlServer,
                ConnectionString = Config.ConnectionString,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true
            });

2.Simple

db.DbFirst.CreateClassFile("c:\\Demo\\1", "Models");

3.Screen tables

db.DbFirst.Where("Student").CreateClassFile("c:\\Demo\\2", "Models");


db.DbFirst.Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\3", "Models");


db.DbFirst.Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\4", "Models");

3.Create SqlSugar Attribute

db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\5", "Models");
using System;
using System.Linq;
using System.Text;
using SqlSugar;

namespace Models
{
    ///<summary>
    ///
    ///</summary>
    [SugarTable("Order")]
    public partial class Order
    {
           public Order(){


           }
           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:False
           /// </summary>           
           [SugarColumn(IsPrimaryKey=true,IsIdentity=true)]
           public int Id {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:False
           /// </summary>           
           public string Name {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:False
           /// </summary>           
           public decimal Price {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public DateTime? CreateTime {get;set;}

    }
}

5.Create database default value

db.DbFirst.IsCreateDefaultValue().CreateClassFile("c:\\Demo\\6", "Demo.Models");

6.Rewrite template

db.DbFirst. SettingClassTemplate(old => { return old;})
            .SettingNamespaceTemplate(old =>{ return old;})
            .SettingPropertyDescriptionTemplate(old =>
            {
                return @"           /// <summary>
                /// Desc_New:{PropertyDescription}
                /// Default_New:{DefaultValue}
                    /// Nullable_New:{IsNullable}
                    /// </summary>";
            })
            .SettingPropertyTemplate(old =>{return old;})
            .SettingConstructorTemplate(old =>{return old; })
        .CreateClassFile("c:\\Demo\\7");

7.Format class name and property name

foreach (var item in db.DbMaintenance.GetTableInfoList())
{
                string entityName = item.Name.ToUpper();/*Format class name*/
                db.MappingTables.Add(entityName , item.Name);
                foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(item.Name))
                {
                    db.MappingColumns.Add(col.DbColumnName.ToUpper() /*Format class property name*/, col.DbColumnName, entityName);
                }
}
db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\8", "Models");
using System;
using System.Linq;
using System.Text;
using SqlSugar;

namespace Models
{
    ///<summary>
    ///
    ///</summary>
    [SugarTable("Order")]
    public partial class ORDER
    {
           public ORDER(){


           }
           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:False
           /// </summary>           
           [SugarColumn(IsPrimaryKey=true,IsIdentity=true,ColumnName="Id")]
           public int ID {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:False
           /// </summary>           
           [SugarColumn(ColumnName="Name")]
           public string NAME {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:False
           /// </summary>           
           [SugarColumn(ColumnName="Price")]
           public decimal PRICE {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           [SugarColumn(ColumnName="CreateTime")]
           public DateTime? CREATETIME {get;set;}

    }
}
Clone this wiki locally