比如項目內已經使用了其它 orm,如 efcore,這樣意味著實體中可能存在 [Key],但它與 FreeSql [Column(IsPrimary = true] 不同。
Q: FreeSql 實體特性為啥這么別扭?
A: 為了考慮一致性用法,全部封裝在 ColumnAttribute 下,這樣用戶使用起來,不用到處 using 或者 回憶特性應該用哪個名字,如自增 [Column(IsIdentity = true)] 即可。
FreeSql 提供 AOP 自定義特性功能,實現與多個 orm 共同擁有一套實體特性,可避免重復定義特性。
以下的示例代碼,FreeSql 使用 EFCore 的實體特性。
fsql.CodeFirst.ConfigEntity<ModelAopConfigEntity>(a => a.Property(b => b.pkid).IsPrimary(true));fsql.Aop.ConfigEntity = (s, e) => {var attr = e.EntityType.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.Schema.TableAttribute), false).FirstOrDefault() as System.ComponentModel.DataAnnotations.Schema.TableAttribute;if (attr != null)e.ModifyResult.Name = attr.Name;
};
fsql.Aop.ConfigEntityProperty = (s, e) => {if (e.Property.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.KeyAttribute), false).Any())e.ModifyResult.IsPrimary = true;
};[System.ComponentModel.DataAnnotations.Schema.Table("xxx")]
class ModelAopConfigEntity {[System.ComponentModel.DataAnnotations.Key][Column(IsPrimary = false)]public int pkid { get; set; }
}
就這樣,FreeSql 的實體特性就可以和 EFCore 那樣設定了。其他自增、樂觀鎖等,依葫蘆畫瓢便是。
優先級
數據庫特性 > 實體特性 > FluantApi(配置特性) > Aop(配置特性)
系列文章導航
(一)入門
(二)自動遷移實體
(三)實體特性
(四)實體特性 Fluent Api
(五)插入數據
(六)批量插入數據
(七)插入數據時忽略列
(八)插入數據時指定列
(九)刪除數據
(十)更新數據
(十一)更新數據 Where
(十二)更新數據時指定列
(十三)更新數據時忽略列
(十四)批量更新數據
(十五)查詢數據
(十六)分頁查詢
(十七)聯表查詢
(十八)導航屬性
(十九)多表查詢
(二十)多表查詢 WhereCascade
(二十一)查詢返回數據
(二十二)Dto 映射查詢
(二十三)分組、聚合
(二十四)Linq To Sql 語法使用介紹
(二十五)延時加載
(二十六)貪婪加載 Include、IncludeMany、Dto、ToList
(二十七)將已寫好的 SQL 語句,與實體類映射進行二次查詢
(二十八)事務
(二十九)Lambda 表達式
(三十)讀寫分離
(三十一)分區分表
(三十二)Aop
(三十三)CodeFirst 類型映射
(三十四)CodeFirst 遷移說明
(三十五)CodeFirst 自定義特性