From fcfeacdc95a8a872c9f9135b1b52592f60120917 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Tue, 12 Jan 2021 12:40:07 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20=E6=8C=87=E5=AE=9A=20D?= =?UTF-8?q?to=20=E6=9F=A5=E8=AF=A2=E5=AF=B9=20c#=20=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.Tests/FreeSql.Tests/UnitTest5.cs | 35 +++++++++++++++++++ FreeSql/Internal/CommonExpression.cs | 11 ++++-- .../Internal/Model/ReadAnonymousTypeInfo.cs | 2 ++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 FreeSql.Tests/FreeSql.Tests/UnitTest5.cs diff --git a/FreeSql.Tests/FreeSql.Tests/UnitTest5.cs b/FreeSql.Tests/FreeSql.Tests/UnitTest5.cs new file mode 100644 index 00000000..c2436f29 --- /dev/null +++ b/FreeSql.Tests/FreeSql.Tests/UnitTest5.cs @@ -0,0 +1,35 @@ +using AME.Helpers; +using FreeSql.DataAnnotations; +using FreeSql.Internal; +using FreeSql.Internal.CommonProvider; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using System.Threading; +using Xunit; +using static FreeSql.Tests.UnitTest1; + +namespace FreeSql.Tests +{ + public class UnitTest5 + { + [Fact] + public void TestUpdateDyWhere() + { + var fsql = g.sqlite; + + var sql = fsql.Update(new { status = "xxx" }) + .Set(a => a.status, "yyy") + .ToSql(); + + Assert.Equal(@"UPDATE ""ts_up_dywhere01"" SET ""status"" = @p_0 +WHERE (""status"" = 'xxx')", sql); + } + class ts_up_dywhere01 + { + public Guid id { get; set; } + public string status { get; set; } + } + } +} diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index 1ee15fca..48dec526 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -280,6 +280,7 @@ namespace FreeSql.Internal CsType = initAssignExp.Expression.Type, MapType = initAssignExp.Expression.Type }; + if (child.Property == null) child.ReflectionField = initExp.Type.GetField(initExp.Bindings[a].Member.Name, BindingFlags.Public | BindingFlags.Instance); parent.Childs.Add(child); ReadAnonymousField(_tables, field, child, ref index, initAssignExp.Expression, select, diymemexp, whereGlobalFilter, findIncludeMany, false); } @@ -399,13 +400,17 @@ namespace FreeSql.Internal var isnull = notRead; for (var b = ctorParmsLength; b < parent.Childs.Count; b++) { - var prop = parent.Childs[b].Property; var dbval = parent.IsEntity ? new ReadAnonymousDbValueRef() : null; var objval = ReadAnonymous(parent.Childs[b], dr, ref index, notRead, dbval, rowIndex, fillIncludeMany); if (isnull == false && parent.IsEntity && dbval.DbValue == null && parent.Table != null && parent.Table.ColumnsByCs.TryGetValue(parent.Childs[b].CsName, out var trycol) && trycol.Attribute.IsPrimary) isnull = true; - if (isnull == false && prop.CanWrite) - prop.SetValue(ret, objval, null); + + if (isnull == false) + { + var prop = parent.Childs[b].Property; + if (prop?.CanWrite == true) prop.SetValue(ret, objval, null); + else if (prop == null) parent.Childs[b].ReflectionField?.SetValue(ret, objval); + } } return isnull ? null : ret; } diff --git a/FreeSql/Internal/Model/ReadAnonymousTypeInfo.cs b/FreeSql/Internal/Model/ReadAnonymousTypeInfo.cs index d2790276..1416caa9 100644 --- a/FreeSql/Internal/Model/ReadAnonymousTypeInfo.cs +++ b/FreeSql/Internal/Model/ReadAnonymousTypeInfo.cs @@ -9,6 +9,7 @@ namespace FreeSql.Internal.Model public class ReadAnonymousTypeInfo { public PropertyInfo Property { get; set; } + public FieldInfo ReflectionField { get; set; } public string CsName { get; set; } public Type CsType { get; set; } public Type MapType { get; set; } @@ -23,6 +24,7 @@ namespace FreeSql.Internal.Model public void CopyTo(ReadAnonymousTypeInfo target) { target.Property = Property; + target.ReflectionField = ReflectionField; target.CsName = CsName; target.CsType = CsType; target.MapType = MapType;