前面有介绍过几篇 CodeFirst 内容文章,有
 * 《(二)自动迁移实体》(https://www.cnblogs.com/FreeSql/p/11531301.html 
<https://www.cnblogs.com/FreeSql/p/11531301.html>) 
 * 《(三)实体特性》(https://www.cnblogs.com/FreeSql/p/11531302.html 
<https://www.cnblogs.com/FreeSql/p/11531302.html>) 
 * 《(四)实体特性 Fluent Api》(https://www.cnblogs.com/FreeSql/p/11531304.html 
<https://www.cnblogs.com/FreeSql/p/11531304.html>) 
 * 《(十八)导航属性》(https://www.cnblogs.com/FreeSql/p/11531352.html 
<https://www.cnblogs.com/FreeSql/p/11531352.html>) 
入门 FreeSql 前这些算是基础教程,需要提前了解,接下来进入 CodeFirst 功能的深入了解。
类型映射是 ORM 最重要的功能之一,FreeSql 支持五大数据库大多数数据库类型,包括 mysql 的 enum/set,pgsql 的 
hstore/jsonb 等等。。除此默认之外,还提供了自定义类型映射。
类型映射,需要考虑写入(我们的写入需要考虑 NoneParameter 和 
Parameter)、读取时的转换工作,这部分扩展对个人使用者而言比较复杂,如有需要请提出您的 issues。
FreeSql 拥有较高容错处理,如:当数据库类型为 bigint 可空,实体类为 int 时,读取数据不会出错。
自定义类型映射(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, 香港 } 
应该不需要解释了吧?
BigInteger 都可以映射使用了,但请注意:仅仅是 CURD 方便, Equals == 判断可以使用,无法实现 + - * / 等操作;
FreeSql.Extensions.JsonMap
上面的 MapType 只能处理有限的类型,JsonMap 是一个扩展包,实现属性对象映射为 varchar 字段,写入时使用 json.net 
序列化,读取时使用 json.net 反序列化。
安装扩展包:
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; } } 
默认类型映射
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 - - 
数组 - - 以上所有类型都支持 - - 
以上类型和长度是默认值,可手工设置,如 string 属性可指定 [Column(DbType = "varchar(max)")]
系列文章导航
 * 
(一)入门 <https://www.cnblogs.com/FreeSql/p/11531300.html>
 * 
(二)自动迁移实体 <https://www.cnblogs.com/FreeSql/p/11531301.html>
 * 
(三)实体特性 <https://www.cnblogs.com/FreeSql/p/11531302.html>
 * 
(四)实体特性 Fluent Api <https://www.cnblogs.com/FreeSql/p/11531304.html>
 * 
(五)插入数据 <https://www.cnblogs.com/FreeSql/p/11531306.html>
 * 
(六)批量插入数据 <https://www.cnblogs.com/FreeSql/p/11531309.html>
 * 
(七)插入数据时忽略列 <https://www.cnblogs.com/FreeSql/p/11531316.html>
 * 
(八)插入数据时指定列 <https://www.cnblogs.com/FreeSql/p/11531318.html>
 * 
(九)删除数据 <https://www.cnblogs.com/FreeSql/p/11531320.html>
 * 
(十)更新数据 <https://www.cnblogs.com/FreeSql/p/11531321.html>
 * 
(十一)更新数据 Where <https://www.cnblogs.com/FreeSql/p/11531324.html>
 * 
(十二)更新数据时指定列 <https://www.cnblogs.com/FreeSql/p/11531327.html>
 * 
(十三)更新数据时忽略列 <https://www.cnblogs.com/FreeSql/p/11531334.html>
 * 
(十四)批量更新数据 <https://www.cnblogs.com/FreeSql/p/11531335.html>
 * 
(十五)查询数据 <https://www.cnblogs.com/FreeSql/p/11531339.html>
 * 
(十六)分页查询 <https://www.cnblogs.com/FreeSql/p/11531341.html>
 * 
(十七)联表查询 <https://www.cnblogs.com/FreeSql/p/11531346.html>
 * 
(十八)导航属性 <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>
 * 
(二十一)查询返回数据 <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>
 * 
(二十五)延时加载 <https://www.cnblogs.com/FreeSql/p/11531395.html>
 * 
(二十六)贪婪加载 Include、IncludeMany、Dto、ToList 
<https://www.cnblogs.com/FreeSql/p/11531404.html>
 * 
(二十七)将已写好的 SQL 语句,与实体类映射进行二次查询 
<https://www.cnblogs.com/FreeSql/p/11531416.html>
 * 
(二十八)事务 <https://www.cnblogs.com/FreeSql/p/11531423.html>
 * 
(二十九)Lambda 表达式 <https://www.cnblogs.com/FreeSql/p/11531425.html>
 * 
(三十)读写分离 <https://www.cnblogs.com/FreeSql/p/11531430.html>
 * 
(三十一)分区分表 <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>
热门工具 换一换
