主鍵(Primary Key)
class Topic { [Column(IsPrimary = true)] public int Id { get; set; } }
約定:
*
當(dāng)沒有指明主鍵時(shí),命名為 id 的字段將成為主鍵;(不區(qū)分大小寫)
*
當(dāng)主鍵是 Guid 類型時(shí),插入時(shí)會自動創(chuàng)建(有序、不重復(fù))的值,所以不需要自己賦值;(支持分布式)
自增(Identity)
class Topic { [Column(IsIdentity = true)] public int Id { get; set; } }
約定:
* 當(dāng)沒有指明主鍵時(shí),標(biāo)記自增的成員將成為主鍵;
唯一鍵(Unique Key)
class AddUniquesInfo { public Guid id { get; set; } [Column(Unique =
"uk_phone")] public string phone { get; set; } [Column(Unique =
"uk_group_index, uk_group_index22")] public string group { get; set; }
[Column(Unique = "uk_group_index")] public int index { get; set; }
[Column(Unique = "uk_group_index22")] public string index22 { get; set; } }
唯一鍵,在多個(gè)屬性指定相同的標(biāo)識,代表聯(lián)合鍵;可使用逗號分割多個(gè) UniqueKey 名。
數(shù)據(jù)庫類型(DbType)
class Topic { [Column(DbType = "varchar(128) NOT NULL")] public string Title {
get; set; } }
可以在類型上指定 NOT NULL,也可以通過 [Column(IsNullable = false)] 設(shè)置;
0.9.12 版本增加了對 MaxLength 特性的解析,避免字符串常用時(shí)的麻煩,上面的 varchar(128) 可改寫成:
class Topic { [MaxLength(128)] public string Title { get; set; } }
說明:由于內(nèi)部按名稱反射查找特性的,所以 MaxLengthAttribute 可以在任意地方定義。 該特性通常定義在
System.ComponentModel.DataAnnotations.MaxLengthAttribute。 如果找不到該類,可自行在項(xiàng)目中定義名稱為
MaxLengthAttribute 的特性類,如下: public class MaxLengthAttribute : Attribute {
public int Length { get; } public MaxLengthAttribute(int length) { this.Length
= length; } }
可空(Nullable)
class Topic { [Column(IsNullable = false)] public string Title { get; set; } }
在不指定 DbType、IsNullable 時(shí),F(xiàn)reeSql 提供默認(rèn)設(shè)定,如:
* int -> not null(不可為空)
* int? -> null(可空)
一般在使用 string 類型時(shí),才需要手工指明是否可空(string 默認(rèn)可空);
忽略(Ignore)
class Topic { [Column(IsIgnore = true)] public string Title { get; set; } }
當(dāng)實(shí)體有屬性不需要映射的時(shí)候使用,內(nèi)部自動忽略了對象的映射;
當(dāng)實(shí)體內(nèi)的屬性不是可接受的類型時(shí),可以不用指定該特定,如下不必要的指定:
class Topic { [Column(IsIgnore = true)] public Topic Parent { get; set; } }
樂觀鎖(RowVersion)
class Topic { public Guid id { get; set; } public string Title { get; set; }
[Column(IsVersion = true)] public int Version { get; set; } }
更新整個(gè)實(shí)體數(shù)據(jù)時(shí),在并發(fā)情況下極容易造成舊數(shù)據(jù)將新的記錄更新。
行級鎖的原理,是利用實(shí)體某字段,如:long version,更新前先查詢數(shù)據(jù),此時(shí) version 為 1,更新時(shí)產(chǎn)生的 SQL 會附加 where
version = 1,當(dāng)修改失敗時(shí)(即 Affrows == 0)拋出異常。
每個(gè)實(shí)體只支持一個(gè)行級鎖屬性。
適用 SetSource 更新,無論使用什么方法更新 version 的值都會增加 1
自定義類型映射(MapType)
class EnumTestMap { public Guid id { get; set; } [Column(MapType =
typeof(string))] public ToStringMapEnum enum_to_string { get; set; }
[Column(MapType = typeof(string))] public ToStringMapEnum?
enumnullable_to_string { get; set; } [Column(MapType = typeof(int))] public
ToStringMapEnum enum_to_int { get; set; } [Column(MapType = typeof(int?))]
public ToStringMapEnum? enumnullable_to_int { get; set; } [Column(MapType =
typeof(string))] public BigInteger biginteger_to_string { get; set; }
[Column(MapType = typeof(string))] public BigInteger?
bigintegernullable_to_string { get; set; } } public enum ToStringMapEnum { 中國人,
abc, 香港 }
應(yīng)該不需要解釋了吧?
BigInteger 都可以映射使用了,但請注意:僅僅是 CURD 方便, Equals == 判斷可以使用,無法實(shí)現(xiàn) + - * / 等操作;
v0.9.15 版本還可以將值對象映射成 typeof(string),安裝擴(kuò)展包:
dotnet add package FreeSql.Extensions.JsonMap
fsql.UseJsonMap(); //開啟功能 class TestConfig { public int clicks { get; set; }
public string title { get; set; } } [Table(Name = "sysconfig")] public class
S_SysConfig<T> { [Column(IsPrimary = true)] public string Name { get; set; }
[JsonMap] public T Config { get; set; } }
字段位置(Position)
適用場景:當(dāng)實(shí)體類繼承時(shí),CodeFirst創(chuàng)建表的字段順序可能不是想要的,通過該特性可以設(shè)置順序。
創(chuàng)建表時(shí)指定字段位置,如:[Column(Position = 1],可為負(fù)數(shù)即反方向位置;
名稱
FreeSql 默認(rèn)使用實(shí)體的類名,或?qū)傩悦c數(shù)據(jù)庫映射,也可以指定映射的名稱;
指定實(shí)體的表名,指定 Name
后,實(shí)體類名變化不影響數(shù)據(jù)庫對應(yīng)的表。FreeSql盡量支持了對多數(shù)據(jù)庫或schema支持,不防試試指定表名為:其他數(shù)據(jù)庫.表名,不同數(shù)據(jù)庫的指定方式有差異,這一點(diǎn)以后深入解答。
[Table(Name = "db2.tb_topic111")] class Topic { //... }
指定實(shí)體的表名,修改為實(shí)體類名。指定數(shù)據(jù)庫舊的表名,修改實(shí)體命名時(shí),同時(shí)設(shè)置此參數(shù)為修改之前的值,CodeFirst才可以正確修改數(shù)據(jù)庫表;否則將視為【創(chuàng)建新表】。
[Table(OldName = "Topic")] class Topic2 { //... }
實(shí)體的屬性也有相同的功能,[Column(Name = "xxx")]
禁用遷移
IFreeSql.CodeFirst.IsAutoSyncStructure 可設(shè)置全局【自動遷移結(jié)構(gòu)】功能,也可通過
FreeSqlBuilder.UseAutoSyncStructure(true) 創(chuàng)建 IFreeSql 的時(shí)候設(shè)置功能。
當(dāng)【實(shí)體類】對應(yīng)的是數(shù)據(jù)庫【視圖】或者其他時(shí),可通過 [Table(DisableSyncStructure = true)] 禁用指定的實(shí)體遷移操作。
[Table(DisableSyncStructure = true)] class ModelDisableSyncStructure {
[Column(IsPrimary = false)] public int pkid { get; set; } }
備注
FreeSql CodeFirst 支持將 c# 代碼內(nèi)的注釋,遷移至數(shù)據(jù)庫的備注。先決條件:
1、實(shí)體類所在程序集,需要開啟 xml 文檔功能;
2、xml 文件必須與程序集同目錄,且文件名:xxx.dll -> xxx.xml;
系列文章導(dǎo)航
*
(一)入門 <https://www.cnblogs.com/FreeSql/p/11531300.html>
*
(二)自動遷移實(shí)體 <https://www.cnblogs.com/FreeSql/p/11531301.html>
*
(三)實(shí)體特性
*
(四)實(shí)體特性 Fluent Api <https://www.cnblogs.com/FreeSql/p/11531304.html>
*
(五)插入數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531306.html>
*
(六)批量插入數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531309.html>
*
(七)插入數(shù)據(jù)時(shí)忽略列 <https://www.cnblogs.com/FreeSql/p/11531316.html>
*
(八)插入數(shù)據(jù)時(shí)指定列 <https://www.cnblogs.com/FreeSql/p/11531318.html>
*
(九)刪除數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531320.html>
*
(十)更新數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531321.html>
*
(十一)更新數(shù)據(jù) Where <https://www.cnblogs.com/FreeSql/p/11531324.html>
*
(十二)更新數(shù)據(jù)時(shí)指定列 <https://www.cnblogs.com/FreeSql/p/11531327.html>
*
(十三)更新數(shù)據(jù)時(shí)忽略列 <https://www.cnblogs.com/FreeSql/p/11531334.html>
*
(十四)批量更新數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531335.html>
*
(十五)查詢數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531339.html>
*
(十六)分頁查詢 <https://www.cnblogs.com/FreeSql/p/11531341.html>
*
(十七)聯(lián)表查詢 <https://www.cnblogs.com/FreeSql/p/11531346.html>
*
(十八)導(dǎo)航屬性 <https://www.cnblogs.com/FreeSql/p/11531352.html>
*
(十九)多表查詢 <https://www.cnblogs.com/FreeSql/p/11531362.html>
*
(二十)多表查詢 WhereCascade <https://www.cnblogs.com/FreeSql/p/11531372.html>
*
(二十一)查詢返回?cái)?shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531376.html>
*
(二十二)Dto 映射查詢 <https://www.cnblogs.com/FreeSql/p/11531381.html>
*
(二十三)分組、聚合 <https://www.cnblogs.com/FreeSql/p/11531384.html>
*
(二十四)Linq To Sql 語法使用介紹 <https://www.cnblogs.com/FreeSql/p/11531392.html>
*
(二十五)延時(shí)加載 <https://www.cnblogs.com/FreeSql/p/11531395.html>
*
(二十六)貪婪加載 Include、IncludeMany、Dto、ToList
<https://www.cnblogs.com/FreeSql/p/11531404.html>
*
(二十七)將已寫好的 SQL 語句,與實(shí)體類映射進(jìn)行二次查詢
<https://www.cnblogs.com/FreeSql/p/11531416.html>
*
(二十八)事務(wù) <https://www.cnblogs.com/FreeSql/p/11531423.html>
*
(二十九)Lambda 表達(dá)式 <https://www.cnblogs.com/FreeSql/p/11531425.html>
*
(三十)讀寫分離 <https://www.cnblogs.com/FreeSql/p/11531430.html>
*
(三十一)分區(qū)分表 <https://www.cnblogs.com/FreeSql/p/11531435.html>
*
(三十二)Aop <https://www.cnblogs.com/FreeSql/p/11531471.html>
*
(三十三)CodeFirst 類型映射 <https://www.cnblogs.com/FreeSql/p/11531543.html>
*
(三十四)CodeFirst 遷移說明 <https://www.cnblogs.com/FreeSql/p/11531550.html>
*
(三十五)CodeFirst 自定義特性 <https://www.cnblogs.com/FreeSql/p/11531576.html>
熱門工具 換一換