From 2401f7a9e334fddd343248d114f7d118ff6d1520 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Fri, 11 Dec 2020 18:48:28 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20=E5=BB=B6=E6=97=B6?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E6=97=B6=E7=BA=A7=E8=81=94=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=20bug=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/FreeSql.DbContext.xml | 9 --- .../RepositoryTests.cs | 72 +++++++++++++++++++ FreeSql.sln | 15 ++++ FreeSql/Internal/UtilsExpressionTree.cs | 6 +- 4 files changed, 91 insertions(+), 11 deletions(-) diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 27909b2e..02eb0609 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -512,14 +512,5 @@ - - - 批量注入 Repository,可以参考代码自行调整 - - - - - - diff --git a/FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs b/FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs index 144a5e9c..684d00ec 100644 --- a/FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs +++ b/FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs @@ -253,6 +253,8 @@ namespace FreeSql.Tests var cts2 = repo.Select.WhereDynamic(cts).IncludeMany(a => a.Goodss).ToList(); cts2[0].Goodss[0].Name += 123; repo.Update(cts2[0]); + cts2[0].Goodss[0].Name += 333; + repo.SaveMany(cts2[0], "Goodss"); } [Table(Name = "EAUNL_OTM_CT")] class Cagetory @@ -270,6 +272,76 @@ namespace FreeSql.Tests public Guid CagetoryId { get; set; } public string Name { get; set; } } + + [Fact] + public void EnableAddOrUpdateNavigateList_OneToMany_lazyloading() + { + var repo = g.sqlite.GetRepository(); + repo.DbContextOptions.EnableAddOrUpdateNavigateList = true; + var cts = new[] { + new CagetoryLD + { + Name = "1", + Goodss = new List(new[] + { + new GoodsLD { Name = "Ʒ1" }, + new GoodsLD { Name = "Ʒ2" }, + new GoodsLD { Name = "Ʒ3" } + }) + }, + new CagetoryLD + { + Name = "2", + Goodss = new List(new[] + { + new GoodsLD { Name = "Ʒ4" }, + new GoodsLD { Name = "Ʒ5" } + }) + } + }; + repo.Insert(cts); + cts[0].Name = "11"; + cts[0].Goodss.Clear(); + cts[1].Name = "22"; + cts[1].Goodss.Clear(); + repo.Update(cts); + cts[0].Name = "111"; + cts[0].Goodss.Clear(); + cts[0].Goodss.Add(new GoodsLD { Name = "Ʒ33" }); + cts[1].Name = "222"; + cts[1].Goodss.Clear(); + cts[1].Goodss.Add(new GoodsLD { Name = "Ʒ55" }); + repo.Update(cts); + + var cts2 = repo.Select.WhereDynamic(cts).IncludeMany(a => a.Goodss).ToList(); + cts2[0].Goodss[0].Name += 123; + repo.Update(cts2[0]); + cts2[0].Goodss[0].Name += 333; + repo.SaveMany(cts2[0], "Goodss"); + + cts2 = repo.Select.WhereDynamic(cts).ToList(); + cts2[0].Goodss[0].Name += 123; + repo.Update(cts2[0]); + cts2[0].Goodss[0].Name += 333; + repo.SaveMany(cts2[0], "Goodss"); + } + [Table(Name = "EAUNL_OTM_CTLD")] + public class CagetoryLD + { + public Guid Id { get; set; } + public string Name { get; set; } + + [Navigate("CagetoryId")] + public virtual List Goodss { get; set; } + } + [Table(Name = "EAUNL_OTM_GDLD")] + public class GoodsLD + { + public Guid Id { get; set; } + public Guid CagetoryId { get; set; } + public string Name { get; set; } + } + [Fact] public void SaveMany_OneToMany() { diff --git a/FreeSql.sln b/FreeSql.sln index fb6b29f9..d159e3c7 100644 --- a/FreeSql.sln +++ b/FreeSql.sln @@ -91,6 +91,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.KingbaseES EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.Firebird", "Providers\FreeSql.Provider.Firebird\FreeSql.Provider.Firebird.csproj", "{101B11D2-7780-4E14-9B72-77F5D69B3DF9}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.ElasticsearchSQL", "Providers\FreeSql.Provider.ElasticsearchSQL\FreeSql.Provider.ElasticsearchSQL.csproj", "{EC6980FD-090D-4BAA-8421-0D4C3D306F0D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -557,6 +559,18 @@ Global {101B11D2-7780-4E14-9B72-77F5D69B3DF9}.Release|x64.Build.0 = Release|Any CPU {101B11D2-7780-4E14-9B72-77F5D69B3DF9}.Release|x86.ActiveCfg = Release|Any CPU {101B11D2-7780-4E14-9B72-77F5D69B3DF9}.Release|x86.Build.0 = Release|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Debug|x64.ActiveCfg = Debug|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Debug|x64.Build.0 = Debug|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Debug|x86.ActiveCfg = Debug|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Debug|x86.Build.0 = Debug|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Release|Any CPU.Build.0 = Release|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Release|x64.ActiveCfg = Release|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Release|x64.Build.0 = Release|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Release|x86.ActiveCfg = Release|Any CPU + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -589,6 +603,7 @@ Global {3D2BD8EC-253A-437F-B4C8-74BC0D91429B} = {2A381C57-2697-427B-9F10-55DA11FD02E4} {CDD6A896-F6DF-44CB-B430-06B383916EB0} = {2A381C57-2697-427B-9F10-55DA11FD02E4} {101B11D2-7780-4E14-9B72-77F5D69B3DF9} = {2A381C57-2697-427B-9F10-55DA11FD02E4} + {EC6980FD-090D-4BAA-8421-0D4C3D306F0D} = {2A381C57-2697-427B-9F10-55DA11FD02E4} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {089687FD-5D25-40AB-BA8A-A10D1E137F98} diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index 06028c6e..7313bcbd 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -925,6 +925,7 @@ namespace FreeSql.Internal { //set 重写 cscode.Append(" ").Append(propSetModification).Append(" set {\r\n") .Append(" base.").Append(pnv.Name).AppendLine(" = value;") + .Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;") .Append(" }\r\n"); } cscode.AppendLine(" }"); @@ -1050,9 +1051,9 @@ namespace FreeSql.Internal if (refprop != null) { cscode.Append(" foreach (var loc1 in base.").Append(pnv.Name).AppendLine(")") - .Append(" loc1.").Append(refprop.Name).AppendLine(" = this;") - .Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;"); + .Append(" loc1.").Append(refprop.Name).AppendLine(" = this;"); } + cscode.Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;"); } else cscode.Append(" throw new Exception(\"").Append(nvref.Exception.Message.Replace("\r\n", "\\r\\n").Replace("\"", "\\\"")).AppendLine("\");"); @@ -1066,6 +1067,7 @@ namespace FreeSql.Internal { //set 重写 cscode.Append(" ").Append(propSetModification).Append(" set {\r\n") .Append(" base.").Append(pnv.Name).AppendLine(" = value;") + .Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;") .Append(" }\r\n"); } cscode.AppendLine(" }");