Code First
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// 属性介绍 // 我们会根据字段的属性生成数据库字段 // IsNullable表示表字段是否可空 // IsIgnore 为true表示不会生成字段到数据库 // IsIdentity表示为自增列 // IsPrimaryKey表示为主键 // Length 表示字段长度 // DecimalDigits 表示字段的精度 4.4 // ColumnDataType 强制设置数据库字段的类型(考虑到切换数据库有些类型其它库没有最新版本支持多个以逗号隔离,比如=“number,int”) dbColumnInfo = new DbColumnInfo() { DbColumnName = "img1", DataType = "text", Length = 1500 } public class CodeTable { [SugarColumn(IsNullable =false ,IsPrimaryKey =true,IsIdentity =true)] public int Id { get; set; } [SugarColumn(Length = 21)] public string Name{ get; set; } [SugarColumn(IsNullable = true)] public bool IsOk { get; set; } public Guid Guid { get; set; } public decimal Decimal { get; set; } [SugarColumn(IsNullable = true)] public DateTime? DateTime { get; set; } [SugarColumn(IsNullable = true)] public double? Dob2 { get; set; } [SugarColumn(Length =10)] public string A { get; set; } } public class CodeTable2 { public int Id { get; set; } public string Name { get; set; } [SugarColumn(IsIgnore =true)] public string TestId { get; set; } } [SugarTable("T_logs")] public class logitem { [SugarColumn(IsPrimaryKey = true, IsIdentity = false, ColumnName = "guid")] public string guid { get; set; } public string type { get; set; } public string log { get; set; } public string addtime { get; set; } } |
SugarColumn
1 2 3 4 5 6 7 8 9 10 11 12 |
[SugarColumn(IsIgnore = true)] [SugarColumn(IsPrimaryKey = true, IsIdentity = false, ColumnName = "guid")] [SugarColumn(IsPrimaryKey = true, IsIdentity = false, ColumnName = "guid", Length = 32)] [SugarColumn(Length = 20)] [SugarColumn(ColumnDataType = "text")] [SugarColumn(ColumnDataType = "text", IsNullable = true)] [SugarColumn(ColumnDataType = "text", IsNullable = true, Length = 255)] [SugarColumn(IsNullable = true, Length = 255)] [SugarColumn(IsNullable = false, DefaultValue = "0", Length = 2)] [SugarColumn(ColumnDataType = "text", DefaultValue = "0", IsNullable = true)] [SugarColumn(ColumnDataType = "text", DefaultValue = "false", IsNullable = true)] |
SugarColumn
text
- SQLite
Text
类型不支持Length
1 2 3 |
[SugarColumn(ColumnDataType = "text", IsNullable = true)] public string supporttag { get; set; } |
SugarColumn
时间
1 2 3 4 5 |
[SugarColumn(IsNullable = false)] public DateTime? createtime { get; set; } [SugarColumn(IsNullable = false)] public DateTime createtime { get; set; } |
SugarColumn INT
- 不能设置默认值否则报错
1 2 3 |
[SugarColumn(IsNullable = false, Length = 2)] public int deployed { get; set; } |
SugarColumn bool / Boolean
- SQLite 没有单独的
Boolean
存储类。相反,布尔值被存储为整数0
(false)和1
(true)
1 2 3 4 5 6 |
[SugarColumn(IsNullable = true, Length = 1)] public bool active { get; set; } // 文本 [SugarColumn(IsNullable = true, Length = 5)] public bool active { get; set; } |
SugarColumn 忽略
1 2 3 |
[SugarColumn(IsIgnore=true)] public bool active { get; set; } |
Insert
代码插入
demo 一
1 2 3 4 5 |
var t2 = db.Insertable(insertObj).ExecuteCommand(); db.Insertable<DemoTest.Database.Main.Entity.Models.demo_guiditem>(item).AddQueue(); db.Insertable(demo_Guiditems); db.Insertable<DemoTest.Database.Main.Entity.Models.urlsguiditem>(item).AddQueue(); |
demo 二
1 2 3 4 5 |
db.Utilities.PageEach(demo_Guiditems, 500, list => { var t2 = db.Insertable(list).ExecuteCommand(); }); |
更新或者插入
1 2 3 4 |
var Students = db.Saveable<Student>(entity).ExecuteReturnEntity(); var count = db.Saveable<Student>(entity).ExecuteCommand(); var t0 = db.Saveable<Student>(entity).ExecuteCommand(); |
Update
更新
只更新某列
- 只更新实体里面的 Name 列(主键要有值,主键是更新条件)
1 2 |
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand(); |
更新代码 1
1 2 3 4 5 |
db.Utilities.PageEach(urlsguiditems_PageEach, 200, list => { var t2 = db.Updateable(list).UpdateColumns(it => new { it.status }).ExecuteCommand(); }); |
PageEach
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// Redis var startIndex = (root.Pagination.PageIndex - 1) * root.Pagination.PageSize; var endIndex = startIndex + root.Pagination.PageSize; // SQL var startIndex = (root.Pagination.PageIndex - 1) * root.Pagination.PageSize; var endIndex = root.Pagination.PageSize; PageExtenions.PageEach(1000, 20, CurrentExecutCount => { try { } catch (Exception ex) { } }); CsharpLazycode.Module.Page.Utilities.PageEach CsharpLazycode.Module.Page.Utilities.PageEach(urlsguiditems, 2, list => { try { } catch (Exception ex) { } }); PageExtenions.PageEach(GoCount, 1000, CurrentExecutCount => { ListStrings = new List<string>(); for (int i = 0; i < CurrentExecutCount; i++) { } }); |
1 |