前面有介紹過幾篇 CodeFirst 內(nèi)容文章,有
* 《(二)自動(dòng)遷移實(shí)體》(https://www.cnblogs.com/FreeSql/p/11531301.html
<https://www.cnblogs.com/FreeSql/p/11531301.html>)
* 《(三)實(shí)體特性》(https://www.cnblogs.com/FreeSql/p/11531302.html
<https://www.cnblogs.com/FreeSql/p/11531302.html>)
* 《(四)實(shí)體特性 Fluent Api》(https://www.cnblogs.com/FreeSql/p/11531304.html
<https://www.cnblogs.com/FreeSql/p/11531304.html>)
* 《(十八)導(dǎo)航屬性》(https://www.cnblogs.com/FreeSql/p/11531352.html
<https://www.cnblogs.com/FreeSql/p/11531352.html>)
入門 FreeSql 前這些算是基礎(chǔ)教程,需要提前了解,接下來進(jìn)入 CodeFirst 功能的深入了解。
類型映射是 ORM 最重要的功能之一,F(xiàn)reeSql 支持五大數(shù)據(jù)庫大多數(shù)數(shù)據(jù)庫類型,包括 mysql 的 enum/set,pgsql 的
hstore/jsonb 等等。。除此默認(rèn)之外,還提供了自定義類型映射。
類型映射,需要考慮寫入(我們的寫入需要考慮 NoneParameter 和
Parameter)、讀取時(shí)的轉(zhuǎn)換工作,這部分?jǐn)U展對個(gè)人使用者而言比較復(fù)雜,如有需要請?zhí)岢瞿?issues。
FreeSql 擁有較高容錯(cuò)處理,如:當(dāng)數(shù)據(jù)庫類型為 bigint 可空,實(shí)體類為 int 時(shí),讀取數(shù)據(jù)不會出錯(cuò)。
自定義類型映射(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) + - * / 等操作;
FreeSql.Extensions.JsonMap
上面的 MapType 只能處理有限的類型,JsonMap 是一個(gè)擴(kuò)展包,實(shí)現(xiàn)屬性對象映射為 varchar 字段,寫入時(shí)使用 json.net
序列化,讀取時(shí)使用 json.net 反序列化。
安裝擴(kuò)展包:
dotnet add package FreeSql.Extensions.JsonMap
fsql.UseJsonMap(); //開啟功能, fsql 為 IFreeSql 對象 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; } }
默認(rèn)類型映射
csharp MySql SqlServer PostgreSQL Oracle Sqlite
bool | bool? bit(1) bit bool number(1) boolean
sbyte | sbyte? tinyint(3) smallint int2 number(4) smallint
short | short? smallint(6) smallint int2 number(6) smallint
int | int? int(11) int int4 number(11) integer
long | long? bigint(20) bigint int8 number(21) integer
byte | byte? tinyint(3) unsigned tinyint int2 number(3) int2
ushort | ushort? smallint(5) unsigned int int4 number(5) unsigned
uint | uint? int(10) unsigned bigint int8 number(10) decimal(10,0)
ulong | ulong? bigint(20) unsigned decimal(20,0) numeric(20,0) number(20)
decimal(21,0)
double | double? double float float8 float(126) double
float | float? float real float4 float(63) float
decimal | decimal? decimal(10,2) decimal(10,2) numeric(10,2) number(10,2)
decimal(10,2)
Guid | Guid? char(36) uniqueidentifier uuid char(36 CHAR) character(36)
TimeSpan | TimeSpan? time time time interval day(2) to second(6) bigint
DateTime | DateTime? datetime datetime timestamp timestamp(6) datetime
DateTimeOffset | DateTimeOffset? - - datetimeoffset timestamp(6) with local
time zone -
Enum | Enum? enum int int4 number(16) mediumint
FlagsEnum | FlagsEnum? set bigint int8 number(32) bigint
byte[] varbinary(255) varbinary(255) bytea blob blob
string varchar(255) nvarchar(255) varchar(255) nvarchar2(255) nvarchar(255)
MygisPoint point - - - -
MygisLineString linestring - - - -
MygisPolygon polygon - - - -
MygisMultiPoint multipoint - - - -
MygisMultiLineString multilinestring - - - -
MygisMultiPolygon multipolygon - - - -
BitArray - - varbit(64) - -
NpgsqlPoint | NpgsqlPoint? - - point - -
NpgsqlLine | NpgsqlLine? - - line - -
NpgsqlLSeg | NpgsqlLSeg? - - lseg - -
NpgsqlBox | NpgsqlBox? - - box - -
NpgsqlPath | NpgsqlPath? - - path - -
NpgsqlPolygon | NpgsqlPolygon? - - polygon - -
NpgsqlCircle | NpgsqlCircle? - - circle - -
(IPAddress Address, int Subnet) | (IPAddress Address, int Subnet)? - - cidr - -
IPAddress - - inet - -
PhysicalAddress - - macaddr - -
NpgsqlRange<int> | NpgsqlRange<int>? - - int4range - -
NpgsqlRange<long> | NpgsqlRange<long>? - - int8range - -
NpgsqlRange<decimal> | NpgsqlRange<decimal>? - - numrange - -
NpgsqlRange<DateTime> | NpgsqlRange<DateTime>? - - tsrange - -
PostgisPoint - - geometry - -
PostgisLineString - - geometry - -
PostgisPolygon - - geometry - -
PostgisMultiPoint - - geometry - -
PostgisMultiLineString - - geometry - -
PostgisMultiPolygon - - geometry - -
PostgisGeometry - - geometry - -
PostgisGeometryCollection - - geometry - -
Dictionary<string, string> - - hstore - -
JToken - - jsonb - -
JObject - - jsonb - -
JArray - - jsonb - -
數(shù)組 - - 以上所有類型都支持 - -
以上類型和長度是默認(rèn)值,可手工設(shè)置,如 string 屬性可指定 [Column(DbType = "varchar(max)")]
系列文章導(dǎo)航
*
(一)入門 <https://www.cnblogs.com/FreeSql/p/11531300.html>
*
(二)自動(dòng)遷移實(shí)體 <https://www.cnblogs.com/FreeSql/p/11531301.html>
*
(三)實(shí)體特性 <https://www.cnblogs.com/FreeSql/p/11531302.html>
*
(四)實(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 類型映射
*
(三十四)CodeFirst 遷移說明 <https://www.cnblogs.com/FreeSql/p/11531550.html>
*
(三十五)CodeFirst 自定義特性 <https://www.cnblogs.com/FreeSql/p/11531576.html>
熱門工具 換一換