1、實體使用自帶特性
1.1 使用用例
對於 CRUD來說只需要配置主鍵和自增列就行了 ,如果類的名稱和數據庫不一樣可以設置數據庫中的名稱
主鍵自增
[SugarTable( "dbstudent" )] //當和數據庫名稱不一樣可以設置表別名 指定表明 public class Student { [SugarColumn(IsPrimaryKey = true , IsIdentity = true )] //數據庫是自增才配自增 public int Id { get ; set ; } public int ? SchoolId { get ; set ; } [SugarColumn(ColumnName = "StudentName" )] //數據庫與實體不一樣設置列名 public string Name { get ; set ; } } |
雙主鍵、複合主鍵、聯合主鍵、多個主鍵
表中字段 如果是自增需要加上 IsIdentity,否則不需要
public class Student { [SugarColumn(IsPrimaryKey = true )] //設置主鍵 public Guid Pk1{ get ; set ; } [SugarColumn(IsPrimaryKey = true )] //設置主鍵 public Guid Pk2{ get ; set ; } public string Name { get ; set ; } } |
無主鍵
當表中無主鍵的時候更新和刪除,需要指定條件,具體用法看文檔
public class Student { public Guid Id{ get ; set ; } public string Name { get ; set ; } } |
總結:只需要針對數據庫中的 自增和主鍵進行設置即可,和數據庫一致
1.2 特性明細
下面是CRUD用到的特性,不包含建表的屬性 (建表看文檔【遷移】)
IsIdentity |
自增列 如果是Oracle請設置OracleSequenceName 設置後和自增一樣使用 |
IsPrimaryKey | 創建主鍵 |
ColumnName | 實體類數據庫列名不一樣設置數據庫列名 |
IsIgnore |
IsIgnore=true表示 ORM 所有操作不處理這列 一般用於數據庫沒有這一列 ORM 非數據庫列加上該特性(配置導航查詢自動IsIgnore=true) |
IsOnlyIgnoreInsert | 插入操作時不處理該列 【插入中忽略】 對數據庫默認值有效 |
IsOnlyIgnoreUpdate |
更新操作不處理該列 【更新中忽略】 |
InsertServerTime | 插入操作:true數據庫時間 |
UpdateServerTime | 更新操作:true數據庫時間 |
InsertSql |
插入根據SQL 等於"0"插入0 等 於"'a'" 插入a 等於 "newid()" 插入newid() |
UpdateSql |
更新根據SQL 等於"0"更新0 等 於"'a'" 更新a 等於 "newid()" 更新newid() |
QuerySql 5.1.4.128+ |
用於單表查詢在沒有使用Select下根據Sql生成 例如等於" Cast( Num_float64 as varchar(500))" 生成的Sql : Cast( Num_float64 as varchar(500)) AS Num_float64 一般用於orm驅動不支持的類型並且自定義類型也失效的情況 只在單表查詢,不使用Select的情況下才生效 注意: 聯表或者SELECT使用 函數或者擴展函數實現 |
OracleSequenceName | 設置Oracle序列,設置後該列等同於自增列 |
MaxParameterNameLength | 一般等於30,用於處理Oracle11 :參數化名字和索引名字超過30 |
建表看文檔【遷移】,這邊只介紹CRUD用到的屬性
2、實體使用自定義特性
下面是實現自定義特性的例子
SqlSugarClient db = new SqlSugarClient( new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true , ConfigureExternalServices = new ConfigureExternalServices() { EntityService = (property, column) => { var attributes = property.GetCustomAttributes( true ); //get all attributes if (attributes.Any(it => it is KeyAttribute)) // by attribute set primarykey { column.IsPrimarykey = true ; //有哪些特性可以看 1.2 特性明細 } //可以寫多個,這邊可以斷點調試 // if (attributes.Any(it => it is NotMappedAttribute)) //{ // column.IsIgnore= true; //} }, EntityNameService = (type, entity) => { var attributes = type.GetCustomAttributes( true ); if (attributes.Any(it => it is TableAttribute)) { entity.DbTableName = (attributes.First(it => it is TableAttribute) as TableAttribute).Name; } } } }); [Table( "student" )] //[SugarTable("student")] public class MyStudent { [Key] //[SugarColumn(IsPrimaryKey =true)] public string Id { get ; set ; } public string Name { get ; set ; } } |
該功能非常強大遠不止這點用法,可以統一處理一些特性邏輯
分享你們的自定義特性實現:
1、方便其它人使用
2、方便自已使用
分享地址: http://www.donet5.com/Ask/9/11065
3、實體不使用特性
3.1 無封裝寫法
根據規則來設置哪個是主鍵,哪個是自增,這樣就不需要在實體加特性了(SqlSugar只需主鍵和自增就可以完成所有操作)
var db= new SqlSugarClient( new ConnectionConfig() { DbType = SqlSugar.DbType.MySql, ConnectionString = Config.ConnectionString, IsAutoCloseConnection = true , ConfigureExternalServices= new ConfigureExternalServices() { EntityService = (t, column) => { if (column.PropertyName.ToLower() == "id" ) //是id的設爲主鍵 { column.IsPrimarykey = true ; if (column.PropertyInfo.PropertyType == typeof ( int )) //是id並且是int的是自增 { column.IsIdentity = true ; } } } , EntityNameService = (type, entity) => { //entity.DbTableName 修改表名 } } }); //根據你自個的邏輯去設置相應的主鍵和自增,也可以從數據庫讀出主鍵和自增來動態設置 //db.DbMaintenance.GetColumnInfosByTableName 可以拿到表的信息 |
3.2 語法糖(5.1.45)
如果大量if else比較難看所以針對指定表進行了一些封
SqlSugarClient db = new SqlSugarClient( new ConnectionConfig() { DbType = DbType.SqlServer, ConnectionString = Config.ConnectionString3, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true , ConfigureExternalServices = new ConfigureExternalServices() { EntityService = (s, p) => { //如果是Order實體進行相關配置 p.IfTable<Order>() .UpdateProperty(it => it.id, it => { it.IsIdentity = true ; it.IsPrimarykey = true ; }) .UpdateProperty(it => it.Name, it => { it.Length = 100; it.IsNullable = true ; }) .OneToOne(it => it.Item, nameof(Order.ItemId)); //如果Custom實體進行相關配置 p.IfTable<Custom>() .UpdateProperty(it => it.id, it => { it.IsIdentity = true ; it.IsPrimarykey = true ; }) .UpdateProperty(it => it.Text, it => { it.DataType= StaticConfig.CodeFirst_BigString; //支持多庫的MaxString用法 }) //好處就是配置導航方便,和針對指定表設置會方便些 //可以結合全局邏輯一起使用,如果下面邏輯和上面有衝突,下面的會覆蓋上面的 //統一設置 nullable等於isnullable=true if (p.IsPrimaryKey== false && new NullabilityInfoContext() .Create(c).WriteState is NullabilityState.Nullable) { p.IsNullable = true ; } }, EntityNameService = (type, entity) => { //entity.DbTableName 修改表名 } } }); //性能說明: //EntityService 相同實體只會執行一次性不需太操作 |
4、遷移-建表
public class CodeFirstTable1 { [SugarColumn(IsIdentity = true , IsPrimaryKey = true )] public int Id { get ; set ; } public string Name { get ; set ; } //ColumnDataType 一般用於單個庫數據庫,如果多庫不建議用 [SugarColumn(ColumnDataType = "Nvarchar(255)" )] public string Text { get ; set ; } [SugarColumn(IsNullable = true )] //可以爲NULL public DateTime CreateTime { get ; set ; } } //建表 db.CodeFirst.SetStringDefaultLength(200).InitTables( typeof (CodeFirstTable1)); |