- 修复 pgsql IList -> JArray 映射;#1092

This commit is contained in:
2881099 2022-04-29 18:56:10 +08:00
parent 10d9090fb4
commit a5723c1773
4 changed files with 82 additions and 10 deletions

View File

@ -538,14 +538,5 @@
<param name="that"></param> <param name="that"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
<summary>
批量注入 Repository可以参考代码自行调整
</summary>
<param name="services"></param>
<param name="globalDataFilter"></param>
<param name="assemblies"></param>
<returns></returns>
</member>
</members> </members>
</doc> </doc>

View File

@ -14,6 +14,16 @@
编号 编号
</summary> </summary>
</member> </member>
<member name="P:FreeSql.Tests.Issues._1092.Test.Name">
<summary>
会员ID
</summary>
</member>
<member name="P:FreeSql.Tests.Issues._1092.Test.Ids">
<summary>
会员ID
</summary>
</member>
<member name="P:FreeSql.Tests.Issues._467.PayOrder.Money"> <member name="P:FreeSql.Tests.Issues._467.PayOrder.Money">
<summary> <summary>
收款金额 收款金额

View File

@ -0,0 +1,66 @@
using FreeSql.DataAnnotations;
using FreeSql;
using System;
using System.Collections.Generic;
using Xunit;
using System.Linq;
using Newtonsoft.Json.Linq;
using NpgsqlTypes;
using Npgsql.LegacyPostgis;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Data.SqlClient;
using kwlib;
using System.Diagnostics;
using System.IO;
using System.Text;
using Newtonsoft.Json;
namespace FreeSql.Tests.Issues
{
public class _1092
{
[Fact]
public void JsonbTest()
{
var fsql = g.pgsql;
fsql.Delete<Test>().Where("1=1").ExecuteAffrows();
var model = new Test() { Name = "hahahah", Ids = new List<long> { 22 } };
var json1 = JsonConvert.SerializeObject(new List<long> { 22 });
var jarray1 = JArray.FromObject(new List<long> { 22 });
//异常:
/*
* Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.String'.
*/
var insertAndOut = fsql.Insert(model)
.ExecuteInserted();
//异常和ExecuteInserted一样的错误
var updateResult = fsql.Update<Test>()
.SetSource(model)
.ExecuteUpdated();
}
[Table(Name = "public.test_issues_1092")]
public class Test
{
[Column(Name = "id", IsPrimary = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
/// 会员ID
/// </summary>
[Column(Name = "name")]
public string Name { get; set; }
/// <summary>
/// 会员ID
/// </summary>
[Column(Name = "ids", MapType = typeof(JArray))]
public List<long> Ids { get; set; }
}
}
}

View File

@ -12,6 +12,7 @@ using System.Data.Common;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Reflection;
using System.Threading; using System.Threading;
namespace FreeSql.PostgreSQL namespace FreeSql.PostgreSQL
@ -70,6 +71,7 @@ namespace FreeSql.PostgreSQL
var MethodJObjectParse = typeof(JObject).GetMethod("Parse", new[] { typeof(string) }); var MethodJObjectParse = typeof(JObject).GetMethod("Parse", new[] { typeof(string) });
var MethodJArrayParse = typeof(JArray).GetMethod("Parse", new[] { typeof(string) }); var MethodJArrayParse = typeof(JArray).GetMethod("Parse", new[] { typeof(string) });
var MethodJsonConvertDeserializeObject = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) }); var MethodJsonConvertDeserializeObject = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) });
var MethodToString = typeof(Utils).GetMethod("ToStringConcat", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(object) }, null);
Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type) => Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type) =>
{ {
switch (type.FullName) switch (type.FullName)
@ -95,7 +97,10 @@ namespace FreeSql.PostgreSQL
return Expression.Return(returnTarget, valueExp); return Expression.Return(returnTarget, valueExp);
} }
if (typeof(IList).IsAssignableFrom(type)) if (typeof(IList).IsAssignableFrom(type))
return Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(valueExp, typeof(string)), Expression.Constant(type, typeof(Type))), type)); return Expression.IfThenElse(
Expression.TypeIs(valueExp, typeof(string)),
Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(valueExp, typeof(string)), Expression.Constant(type, typeof(Type))), type)),
Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(Expression.Call(MethodToString, valueExp), typeof(string)), Expression.Constant(type, typeof(Type))), type)));
return null; return null;
}); });
} }