FreeSql
FreeSql copied to clipboard
JsonMap 字段需要控制序列化文本长度
问题描述及重现步骤:
如果 JsonMap 标记的字段序列化后文本太长的话,会导致数据添加或者修改失败
数据库的具体版本
mysql
问题分析
默认 JsonMap 标记的字段 MySQL 中映射为 text 字段,text 的长度为 65k,当序列化的对象长度超过 65k 入库就会发生异常。
抱歉,默认已经映射为 longtext 了,是我自己这里代码的问题。
能增加自定义映射类型吗?比如mysql,我想映射为Json类型。[JsonMap("mysql=json")],其它数据库就用默认的longtext
@wojiaofsp
fsql.Aop.ConfigEntityProperty += (_, e) =>
{
if (e.Property.PropertyType == typeof(JsonClass))
{
e.ModifyResult.StringLength = -1;
e.ModifyResult.MapType = typeof(string);
if (fsql.Ado.DataType == DataType.MySql)
{
e.ModifyResult.DbType = "json";
}
}
};
FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(JsonClass), new String_JsonClass());
class Product
{
public Guid id { get; set; }
public JsonClass json { get; set; }
}
class JsonClass
{
public int a { get; set; }
public int b { get; set; }
}
class String_JsonClass : TypeHandler<JsonClass>
{
public override object Serialize(JsonClass value)
{
return JsonConvert.SerializeObject(value);
}
public override JsonClass Deserialize(object value)
{
return JsonConvert.DeserializeObject<JsonClass>((string)value);
}
}