Skip to content

Commit b542edf

Browse files
committed
- 修复 ISelect.InsertInto 设置别名时无法使用的错误;#576
1 parent ab130ac commit b542edf

File tree

4 files changed

+97
-10
lines changed

4 files changed

+97
-10
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using FreeSql.DataAnnotations;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.ComponentModel;
6+
using System.ComponentModel.DataAnnotations;
7+
using System.Diagnostics;
8+
using System.Reflection;
9+
using System.Text;
10+
using System.Threading;
11+
using Xunit;
12+
13+
namespace FreeSql.Tests.Issues
14+
{
15+
public class _576
16+
{
17+
[ExpressionCall]
18+
public static class _576Extensions
19+
{
20+
public static ThreadLocal<ExpressionCallContext> expContext = new ThreadLocal<ExpressionCallContext>();
21+
22+
/// <summary>
23+
/// 自定义表达式树函数解析
24+
/// </summary>
25+
/// <param name="that"></param>
26+
/// <param name="withinCode"></param>
27+
/// <returns></returns>
28+
public static string ToNormalWithinCodeGuid([RawValue] this Guid that, string withinCode)
29+
{
30+
expContext.Value.Result = $"{expContext.Value.ParsedContent["withinCode"]} || '{that.ToString("N")}'";
31+
return null;
32+
}
33+
}
34+
35+
[Fact]
36+
public void InsertInto()
37+
{
38+
IFreeSql fsql = g.oracle;
39+
40+
41+
fsql.Delete<SysRole>().Where("1=1").ExecuteAffrows();
42+
var id = Guid.NewGuid().ToString();
43+
fsql.Insert(new SysRole { Guid = id, RoleName = "role1", Sort = 1 }).ExecuteAffrows();
44+
45+
Assert.Equal(1, fsql.Select<SysRole>().Where(a => a.Guid == id).InsertInto("", a => new SysRole
46+
{
47+
Guid = "'x123123dasfafd'",
48+
RoleName = Guid.NewGuid().ToNormalWithinCodeGuid(a.RoleName),
49+
Sort = a.Sort
50+
}));
51+
52+
var item = fsql.Select<SysRole>().Where(a => a.Guid == "x123123dasfafd").First();
53+
Assert.NotNull(item);
54+
Assert.True(item.RoleName.StartsWith("role1") && item.RoleName.Length == 37);
55+
Assert.Equal(item.Sort, 1);
56+
}
57+
58+
[Table(Name = "issues_576_SysRole")]
59+
public partial class SysRole
60+
{
61+
62+
/// <summary>
63+
/// GUID
64+
/// </summary>
65+
[JsonProperty, Column(Name = "GUID", DbType = "VARCHAR2(60 BYTE)", IsPrimary = true)]
66+
public string Guid { get; set; }
67+
68+
/// <summary>
69+
/// 角色名称
70+
/// </summary>
71+
[JsonProperty, Column(Name = "ROLE_NAME", DbType = "NVARCHAR2(40)")]
72+
public string RoleName { get; set; }
73+
74+
/// <summary>
75+
/// 角色排序
76+
/// </summary>
77+
[JsonProperty, Column(Name = "SORT")]
78+
public int Sort { get; set; }
79+
}
80+
}
81+
}

FreeSql/Internal/CommonProvider/SelectProvider/Select0ProviderReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ protected string InternalGetInsertIntoToSql<TTargetEntity>(string tableName, Exp
786786
}
787787
var selectField = string.Join(", ", childs.Select(a => a.DbField));
788788
var selectSql = this.ToSql(selectField);
789-
var insertField = string.Join(", ", childs.Select(a => _commonUtils.QuoteSqlName(tb.Columns[a.CsName].Attribute.Name)));
789+
var insertField = string.Join(", ", childs.Select(a => _commonUtils.QuoteSqlName(tb.ColumnsByCs[a.CsName].Attribute.Name)));
790790
var sql = $"INSERT INTO {_commonUtils.QuoteSqlName(tableName)}({insertField})\r\n{selectSql}";
791791
return sql;
792792
}

0 commit comments

Comments
 (0)