应用笔记 · 2023年3月16日

.Net SqlSugar ORM 表索引的使用——唯一、排序、占位符等

 创建索引

分表的用户注意了:请升级到5.0.8.6-preview03修复了分表问题

    //普通索引
    [SugarIndex("index_codetable1_name",nameof(CodeFirstTable1.Name),OrderByType.Asc)]
     
    //唯一索引 (true表示唯一索引 或者叫 唯一约束)
    [SugarIndex("unique_codetable1_CreateTime", nameof(CodeFirstTable1.CreateTime), OrderByType.Desc,true)]
     
    //复合普通索引
    [SugarIndex("index_codetable1_nameid", nameof(CodeFirstTable1.Name), OrderByType.Asc,
                        nameof(CodeFirstTable1.Id),    OrderByType.Desc)]
   
    public class CodeFirstTable1
    {
        [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public int Id { getset; }
        public string Name { getset; }
        [SugarColumn(ColumnDataType = "Nvarchar(255)")]//custom
        public string Text { getset; }
        [SugarColumn(IsNullable = true)]
        public DateTime CreateTime { getset; }
    }
    //分表的用户注意了:请升级到5.0.8.6-preview03修复了分表问题

给索引名添加占位符

//占位符 {table} {db}
//请升级到 5.0.2.3preivew04
//使用 {db}  进行占位符替换,小写不要有空格
[SugarIndex("{db}index_codetable1_name",nameof(CodeFirstTable1.Name),OrderByType.Asc)]
//表名占位符(自动分表不需要加这个自动的)
[SugarIndex("index_{table}_name",nameof(CodeFirstTable1.Name),OrderByType.Asc)]

索引include 5.1.3.31-preview11

    //不要有空格并且小写等于 include(name,id)
    [SugarIndex("IndexUnituadfasf1_longx{include:name,id}", nameof(longx), OrderByType.Asc)]
    public class Unituadfasf1
    {
        public ulong longx { getset; }
        public int id { getset; }
        public string name { getset; }
    }