FreeSql (thirty-three) CodeFirst type mapping

Front introduced several CodeFirst content articles,

These pre-entry FreeSql be based tutorial, you need to know ahead of time, then proceed to understand CodeFirst function.

Type mapping is one of the most important functions of ORM, FreeSql support for most database types five databases, including the mysql enum / set, pgsql of hstore / jsonb and so on. . In addition to this default, but also it provides a custom type mappings.

Type mapping, consider the write (we need to consider writing NoneParameter and Parameter), conversion work when reading, this part of the extension to the more complex in terms of individual users, if necessary, please raise your issues.

FreeSql has a higher fault tolerance, such as: when the database is bigint be empty, int entity class, the read data is not an error.

Custom type mapping (the 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, 香港 }

You should not need to explain it?

BigInteger can be mapped using, but please note: only convenient CURD, Equals == judge can use, can not be achieved + - * / and other operations;

FreeSql.Extensions.JsonMap

The above process only a limited type MapType, JsonMap an expansion pack, to achieve the object mapping property varchar field, using the write json.net serialization, deserialization using json.net reading.

Installation Expansion Pack:

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; }
}

The default type mappings

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)
head | head? 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
Flag senum | Flag scenes will? 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)")]

Guess you like

Origin www.cnblogs.com/FreeSql/p/11531543.html