From 3d120af6df9b115f871ef19ea1c0dd9228c242c7 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Fri, 15 Jul 2022 19:02:22 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20DbContext/Repository?= =?UTF-8?q?=20Primary=20decimal=20=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86=20k?= =?UTF-8?q?ey=20=E7=B2=BE=E5=BA=A6=E5=A4=84=E7=90=86=20bug=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FreeSql.Tests/Internal/EntityUtilTest.cs | 176 ++++++++++++++++++ FreeSql/Extensions/EntityUtilExtensions.cs | 57 +++--- 2 files changed, 212 insertions(+), 21 deletions(-) create mode 100644 FreeSql.Tests/FreeSql.Tests/Internal/EntityUtilTest.cs diff --git a/FreeSql.Tests/FreeSql.Tests/Internal/EntityUtilTest.cs b/FreeSql.Tests/FreeSql.Tests/Internal/EntityUtilTest.cs new file mode 100644 index 00000000..e7dc853d --- /dev/null +++ b/FreeSql.Tests/FreeSql.Tests/Internal/EntityUtilTest.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Data.SqlClient; +using Xunit; +using FreeSql.Extensions.EntityUtil; + +namespace FreeSql.Tests.Internal +{ + + public class EntityUtilTest + { + [Fact] + public void GetEntityKeyString() + { + var fsql = g.sqlserver; + + var t1 = new GEKS_01 { }; + Assert.Equal("", fsql.GetEntityKeyString(typeof(GEKS_01), t1, false)); + Assert.Equal(Guid.Empty, t1.id); + + var t2 = Guid.NewGuid(); + var t3 = new GEKS_01 { id = t2 }; + Assert.Equal(t2.ToString().ToString(), fsql.GetEntityKeyString(typeof(GEKS_01), t3, false)); + Assert.Equal(t2, t3.id); + + var t4 = new GEKS_01 { }; + Assert.Equal(fsql.GetEntityKeyString(typeof(GEKS_01), t1, true).Length, 36); + Assert.NotEqual(Guid.Empty, t1.id); + + + var t5 = new GEKS_02 { }; + Assert.Equal("0", fsql.GetEntityKeyString(typeof(GEKS_02), t5, false)); + Assert.Equal(0, t5.id); + + var t6 = new GEKS_02 { id = 100 }; + Assert.Equal("100", fsql.GetEntityKeyString(typeof(GEKS_02), t6, false)); + Assert.Equal(100, t6.id); + + + var t7 = new GEKS_03 { }; + Assert.Equal("0", fsql.GetEntityKeyString(typeof(GEKS_03), t7, false)); + Assert.Equal(0, t7.id); + + var t8 = new GEKS_03 { id = 100 }; + Assert.Equal("100", fsql.GetEntityKeyString(typeof(GEKS_03), t8, false)); + Assert.Equal(100, t8.id); + + + var t9 = new GEKS_04 { }; + Assert.Equal("", fsql.GetEntityKeyString(typeof(GEKS_04), t9, false)); + Assert.Null(t9.id); + + var t10 = new GEKS_04 { id = "admin" }; + Assert.Equal("admin", fsql.GetEntityKeyString(typeof(GEKS_04), t10, false)); + Assert.Equal("admin", t10.id); + + + var t11 = new GEKS_05 { }; + Assert.Equal("0", fsql.GetEntityKeyString(typeof(GEKS_05), t11, false)); + Assert.Equal(0, t11.id); + + var t12 = new GEKS_05 { id = 100 }; + Assert.Equal("100", fsql.GetEntityKeyString(typeof(GEKS_05), t12, false)); + Assert.Equal(100, t12.id); + + var t13 = new GEKS_05 { id = 100.000000M }; + Assert.Equal("100", fsql.GetEntityKeyString(typeof(GEKS_05), t13, false)); + Assert.Equal(100, t13.id); + } + + class GEKS_01 + { + public Guid id { get; set; } + } + class GEKS_02 + { + public int id { get; set; } + } + class GEKS_03 + { + public long id { get; set; } + } + class GEKS_04 + { + public string id { get; set; } + } + class GEKS_05 + { + public decimal id { get; set; } + } + + [Fact] + public void GetEntityKeyStringNullable() + { + var fsql = g.sqlserver; + + var t1 = new GEKS_06 { }; + Assert.Equal("", fsql.GetEntityKeyString(typeof(GEKS_06), t1, false)); + Assert.Null(t1.id); + + var t2 = Guid.NewGuid(); + var t3 = new GEKS_06 { id = t2 }; + Assert.Equal(t2.ToString().ToString(), fsql.GetEntityKeyString(typeof(GEKS_06), t3, false)); + Assert.Equal(t2, t3.id); + + var t4 = new GEKS_06 { }; + Assert.Equal(fsql.GetEntityKeyString(typeof(GEKS_06), t1, true).Length, 36); + Assert.NotNull(t1.id); + + + var t5 = new GEKS_07 { }; + Assert.Equal("", fsql.GetEntityKeyString(typeof(GEKS_07), t5, false)); + Assert.Null(t5.id); + + var t6 = new GEKS_07 { id = 100 }; + Assert.Equal("100", fsql.GetEntityKeyString(typeof(GEKS_07), t6, false)); + Assert.Equal(100, t6.id); + + + var t7 = new GEKS_08 { }; + Assert.Equal("", fsql.GetEntityKeyString(typeof(GEKS_08), t7, false)); + Assert.Null(t7.id); + + var t8 = new GEKS_08 { id = 100 }; + Assert.Equal("100", fsql.GetEntityKeyString(typeof(GEKS_08), t8, false)); + Assert.Equal(100, t8.id); + + + var t9 = new GEKS_09 { }; + Assert.Equal("", fsql.GetEntityKeyString(typeof(GEKS_09), t9, false)); + Assert.Null(t9.id); + + var t10 = new GEKS_09 { id = "admin" }; + Assert.Equal("admin", fsql.GetEntityKeyString(typeof(GEKS_09), t10, false)); + Assert.Equal("admin", t10.id); + + + var t11 = new GEKS_10 { }; + Assert.Equal("", fsql.GetEntityKeyString(typeof(GEKS_10), t11, false)); + Assert.Null(t11.id); + + var t12 = new GEKS_10 { id = 100 }; + Assert.Equal("100", fsql.GetEntityKeyString(typeof(GEKS_10), t12, false)); + Assert.Equal(100, t12.id); + + var t13 = new GEKS_10 { id = 100.000000M }; + Assert.Equal("100", fsql.GetEntityKeyString(typeof(GEKS_10), t13, false)); + Assert.Equal(100, t13.id); + } + + class GEKS_06 + { + public Guid? id { get; set; } + } + class GEKS_07 + { + public int? id { get; set; } + } + class GEKS_08 + { + public long? id { get; set; } + } + class GEKS_09 + { + public string? id { get; set; } + } + class GEKS_10 + { + public decimal? id { get; set; } + } + } +} diff --git a/FreeSql/Extensions/EntityUtilExtensions.cs b/FreeSql/Extensions/EntityUtilExtensions.cs index b19f1316..92a3ff54 100644 --- a/FreeSql/Extensions/EntityUtilExtensions.cs +++ b/FreeSql/Extensions/EntityUtilExtensions.cs @@ -14,6 +14,7 @@ namespace FreeSql.Extensions.EntityUtil static readonly MethodInfo MethodStringBuilderAppend = typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(object) }); static readonly MethodInfo MethodStringBuilderToString = typeof(StringBuilder).GetMethod("ToString", new Type[0]); + static readonly MethodInfo MethodDecimalToString = typeof(decimal).GetMethod("ToString", new[] { typeof(string) }); static readonly PropertyInfo MethodStringBuilderLength = typeof(StringBuilder).GetProperty("Length"); static readonly MethodInfo MethodStringConcat = typeof(string).GetMethod("Concat", new Type[] { typeof(object) }); static readonly MethodInfo MethodFreeUtilNewMongodbId = typeof(FreeUtil).GetMethod("NewMongodbId"); @@ -59,11 +60,11 @@ namespace FreeSql.Extensions.EntityUtil if (pks[a].CsType == typeof(Guid?)) newguid = Expression.Convert(newguid, typeof(Guid?)); expthen = Expression.Block( new Expression[]{ - Expression.Assign(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), newguid), - a > 0 ? Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(splitString)) : null, - Expression.Call(var2Sb, MethodStringBuilderAppend, - Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), typeof(object)) - ) + Expression.Assign(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), newguid), + a > 0 ? Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(splitString)) : null, + Expression.Call(var2Sb, MethodStringBuilderAppend, + Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), typeof(object)) + ) }.Where(c => c != null).ToArray() ); } @@ -82,6 +83,34 @@ namespace FreeSql.Extensions.EntityUtil { expthen = Expression.Assign(var3IsNull, Expression.Constant(true)); } + Expression propExp = Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]); + var blockExps = new List(); + if (a > 0) blockExps.Add(Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(splitString))); + if (pks[a].CsType == typeof(decimal)) + { + blockExps.Add( + Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Convert( + Expression.Call(propExp, MethodDecimalToString, Expression.Constant("g0", typeof(string))) + , typeof(object))) + ); + } + else if (pks[a].CsType == typeof(decimal?)) + { + blockExps.Add( + Expression.IfThen( + Expression.NotEqual(propExp, Expression.Default(pks[a].CsType)), + Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Convert( + Expression.Call(Expression.Convert(propExp, typeof(decimal)), MethodDecimalToString, Expression.Constant("g0", typeof(string))) + , typeof(object))) + ) + ); + } + else + { + blockExps.Add( + Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Convert(propExp, typeof(object))) + ); + } if (pks[a].Attribute.IsIdentity || isguid || pks[a].CsType == typeof(string) || pks[a].CsType.IsNullableType()) { exps.Add( @@ -93,14 +122,7 @@ namespace FreeSql.Extensions.EntityUtil Expression.IsTrue(parm2), expthen ), - Expression.Block( - new Expression[]{ - a > 0 ? Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(splitString)) : null, - Expression.Call(var2Sb, MethodStringBuilderAppend, - Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), typeof(object)) - ) - }.Where(c => c != null).ToArray() - ) + Expression.Block(blockExps.ToArray()) ) ) ); @@ -110,14 +132,7 @@ namespace FreeSql.Extensions.EntityUtil exps.Add( Expression.IfThen( Expression.IsFalse(var3IsNull), - Expression.Block( - new Expression[]{ - a > 0 ? Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(splitString)) : null, - Expression.Call(var2Sb, MethodStringBuilderAppend, - Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), typeof(object)) - ) - }.Where(c => c != null).ToArray() - ) + Expression.Block(blockExps.ToArray()) ) ); }