mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-03-17 03:20:59 +08:00
Updated 类型映射 (markdown)
45
类型映射.md
45
类型映射.md
@@ -83,47 +83,38 @@ fsql.Select<Table>().Where(a => a.Options.Value1 == 100 && a.Options.Value2 == "
|
||||
## TypeHandlers(自定义)
|
||||
|
||||
```csharp
|
||||
FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(JsonClass), new String_JsonClass());
|
||||
FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(JsonPoco), new JsonPocoTypeHandler());
|
||||
FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(DateOnly), new DateOnlyTypeHandler());
|
||||
FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(DateTimeOffset), new DateTimeOffsetTypeHandler());
|
||||
|
||||
class Product
|
||||
{
|
||||
public Guid id { get; set; }
|
||||
[Column(MapType = typeof(string), StringLength = -1)]
|
||||
public JsonClass json { get; set; }
|
||||
public JsonPoco json { get; set; }
|
||||
public DateOnly date { get; set; }
|
||||
public DateTimeOffset dateTimeOffset { get; set; }
|
||||
}
|
||||
class JsonClass
|
||||
class JsonPoco
|
||||
{
|
||||
public int a { get; set; }
|
||||
public int b { get; set; }
|
||||
}
|
||||
class String_JsonClass : TypeHandler<JsonClass>
|
||||
class JsonPocoTypeHandler : TypeHandler<JsonPoco>
|
||||
{
|
||||
public override object Serialize(JsonClass value)
|
||||
{
|
||||
return JsonConvert.SerializeObject(value);
|
||||
}
|
||||
public override JsonClass Deserialize(object value)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<JsonClass>((string)value);
|
||||
}
|
||||
public override object Serialize(JsonPoco value) => JsonConvert.SerializeObject(value);
|
||||
public override JsonPoco Deserialize(object value) => JsonConvert.DeserializeObject<JsonPoco>((string)value);
|
||||
public override void FluentApi(FluentColumn col) => col.MapType(typeof(string)).StringLength(-1);
|
||||
}
|
||||
|
||||
class Class1
|
||||
class DateOnlyTypeHandler : TypeHandler<DateOnly>
|
||||
{
|
||||
//自定义 DateTimeOffset
|
||||
[Column(MapType = typeof(string), DbType = "datetime")]
|
||||
public DateTimeOffset Join { get; set; }
|
||||
public override object Serialize(DateOnly value) => value.ToString("yyyy-MM-dd");
|
||||
public override DateOnly Deserialize(object value) => DateOnly.TryParse(string.Concat(value), out var trydo) ? trydo : DateOnly.MinValue;
|
||||
public override void FluentApi(FluentColumn col) => col.MapType(typeof(string)).StringLength(12);
|
||||
}
|
||||
class DateTimeOffsetTypeHandler : TypeHandler<DateTimeOffset>
|
||||
{
|
||||
public override object Serialize(DateTimeOffset value)
|
||||
{
|
||||
return value.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
public override DateTimeOffset Deserialize(object value)
|
||||
{
|
||||
return DateTimeOffset.TryParse((string)value, out var dts) ? dts : DateTimeOffset.MinValue;
|
||||
}
|
||||
public override object Serialize(DateTimeOffset value) => value.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss");
|
||||
public override DateTimeOffset Deserialize(object value) => DateTimeOffset.TryParse((string)value, out var dts) ? dts : DateTimeOffset.MinValue;
|
||||
public override void FluentApi(FluentColumn col) => col.MapType(typeof(string)).DbType("datetime");
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user