- 优化 指定 Dto 查询对 c# 字段的支持;

This commit is contained in:
2881099 2021-01-12 12:40:07 +08:00
parent 1e6c6fc9a8
commit fcfeacdc95
3 changed files with 45 additions and 3 deletions

View File

@ -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<ts_up_dywhere01>(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; }
}
}
}

View File

@ -280,6 +280,7 @@ namespace FreeSql.Internal
CsType = initAssignExp.Expression.Type, CsType = initAssignExp.Expression.Type,
MapType = 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); parent.Childs.Add(child);
ReadAnonymousField(_tables, field, child, ref index, initAssignExp.Expression, select, diymemexp, whereGlobalFilter, findIncludeMany, false); ReadAnonymousField(_tables, field, child, ref index, initAssignExp.Expression, select, diymemexp, whereGlobalFilter, findIncludeMany, false);
} }
@ -399,13 +400,17 @@ namespace FreeSql.Internal
var isnull = notRead; var isnull = notRead;
for (var b = ctorParmsLength; b < parent.Childs.Count; b++) for (var b = ctorParmsLength; b < parent.Childs.Count; b++)
{ {
var prop = parent.Childs[b].Property;
var dbval = parent.IsEntity ? new ReadAnonymousDbValueRef() : null; var dbval = parent.IsEntity ? new ReadAnonymousDbValueRef() : null;
var objval = ReadAnonymous(parent.Childs[b], dr, ref index, notRead, dbval, rowIndex, fillIncludeMany); 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) 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; 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; return isnull ? null : ret;
} }

View File

@ -9,6 +9,7 @@ namespace FreeSql.Internal.Model
public class ReadAnonymousTypeInfo public class ReadAnonymousTypeInfo
{ {
public PropertyInfo Property { get; set; } public PropertyInfo Property { get; set; }
public FieldInfo ReflectionField { get; set; }
public string CsName { get; set; } public string CsName { get; set; }
public Type CsType { get; set; } public Type CsType { get; set; }
public Type MapType { get; set; } public Type MapType { get; set; }
@ -23,6 +24,7 @@ namespace FreeSql.Internal.Model
public void CopyTo(ReadAnonymousTypeInfo target) public void CopyTo(ReadAnonymousTypeInfo target)
{ {
target.Property = Property; target.Property = Property;
target.ReflectionField = ReflectionField;
target.CsName = CsName; target.CsName = CsName;
target.CsType = CsType; target.CsType = CsType;
target.MapType = MapType; target.MapType = MapType;