From 5a691288071a62f6acc9db3a35402b7bce8e5561 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Tue, 21 May 2019 10:39:35 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20=E5=BB=B6=E6=97=B6?= =?UTF-8?q?=E5=AF=BC=E8=88=AA=E5=B1=9E=E6=80=A7=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=8F=90=E9=86=92=EF=BC=8C=E5=BD=93=E6=97=A0=E6=B3=95=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E9=94=99=E8=AF=AF=EF=BC=8C=E8=BD=AC=E5=88=B0=E9=87=8D?= =?UTF-8?q?=E5=86=99=E7=B1=BB=20get=20=E6=97=B6=E6=8A=9B=E5=87=BA=EF=BC=88?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=BB=B6=E6=97=B6=E5=AF=BC=E8=88=AA=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=EF=BC=8C=E4=B8=8E=E6=99=AE=E9=80=9A=E5=AF=BC=E8=88=AA?= =?UTF-8?q?=E4=B8=80=E8=B5=B7=E4=BD=BF=E7=94=A8=EF=BC=89=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/FreeSql.csproj | 2 +- .../SelectProvider/Select0Provider.cs | 3 +- FreeSql/Internal/UtilsExpressionTree.cs | 42 ++++++++++++------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/FreeSql/FreeSql.csproj b/FreeSql/FreeSql.csproj index 421a2a47..66288a1a 100644 --- a/FreeSql/FreeSql.csproj +++ b/FreeSql/FreeSql.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.5.21 + 0.5.22 true YeXiangQin FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite. diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs index 4b029f4f..53463150 100644 --- a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs +++ b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs @@ -704,7 +704,8 @@ namespace FreeSql.Internal.CommonProvider { Expression.Add(dataIndexExp, Expression.Constant(1)) ); else { - readExpAssign = Expression.Call(Utils.MethodExecuteArrayRowReadClassOrTuple, new Expression[] { Expression.Constant(null, typeof(string)), Expression.Constant(prop.PropertyType), Expression.Constant(null, typeof(int[])), rowExp, dataIndexExp, Expression.Constant(_commonUtils) }); + var propLazyType = _commonUtils.GetTableByEntity(prop.PropertyType)?.TypeLazy ?? prop.PropertyType; + readExpAssign = Expression.Call(Utils.MethodExecuteArrayRowReadClassOrTuple, new Expression[] { Expression.Constant(null, typeof(string)), Expression.Constant(propLazyType), Expression.Constant(null, typeof(int[])), rowExp, dataIndexExp, Expression.Constant(_commonUtils) }); } } blockExp.AddRange(new Expression[] { diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index f1b64a84..eb5726d5 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -467,13 +467,13 @@ namespace FreeSql.Internal { if (trycol != null && trycol.CsType.NullableTypeOrThis() != trytb.Primarys[a].CsType) { nvref.Exception = new Exception($"【OneToMany】导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{trytb.CsName}.{trytb.Primarys[a].CsName} 和 {tbref.CsName}.{trycol.CsName} 类型不一致"); trytb.AddOrUpdateTableRef(pnv.Name, nvref); - if (isLazy) throw nvref.Exception; + //if (isLazy) throw nvref.Exception; continue; } if (trycol == null) { nvref.Exception = new Exception($"【OneToMany】导航属性 {trytbTypeName}.{pnv.Name} 在 {tbref.CsName} 中没有找到对应的字段,如:{findtrytb}{findtrytbPkCsName}、{findtrytb}_{findtrytbPkCsName}" + (refprop == null ? "" : $"、{refprop.Name}{findtrytbPkCsName}、{refprop.Name}_{findtrytbPkCsName}")); trytb.AddOrUpdateTableRef(pnv.Name, nvref); - if (isLazy) throw nvref.Exception; + //if (isLazy) throw nvref.Exception; continue; } } @@ -481,7 +481,7 @@ namespace FreeSql.Internal { nvref.Columns.Add(trytb.Primarys[a]); nvref.RefColumns.Add(trycol); - if (isLazy) { + if (isLazy && nvref.Exception == null) { if (a > 0) lmbdWhere.Append(" && "); lmbdWhere.Append("a.").Append(trycol.CsName).Append(" == this.").Append(trytb.Primarys[a].CsName); @@ -506,13 +506,19 @@ namespace FreeSql.Internal { .Append(" public override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {"); if (vp.Item2) { //get 重写 cscode.Append(" get {\r\n") - .Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {") - .Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propElementType.IsNested ? $"{propElementType.DeclaringType.Namespace}.{propElementType.DeclaringType.Name}.{propElementType.Name}" : $"{propElementType.Namespace}.{propElementType.Name}").Append(">().Where(a => ").Append(lmbdWhere.ToString()).AppendLine(").ToList();"); - if (refprop != null) { - cscode.Append(" foreach (var loc1 in base.").Append(pnv.Name).AppendLine(")") - .Append(" loc1.").Append(refprop.Name).AppendLine(" = this;"); - } - cscode.Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;") + .Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {"); + + if (nvref.Exception == null) { + cscode.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propElementType.IsNested ? $"{propElementType.DeclaringType.Namespace}.{propElementType.DeclaringType.Name}.{propElementType.Name}" : $"{propElementType.Namespace}.{propElementType.Name}").Append(">().Where(a => ").Append(lmbdWhere.ToString()).AppendLine(").ToList();"); + 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;"); + } + } else + cscode.Append(" throw new Exception(\"").Append(nvref.Exception.Message.Replace("\r\n", "\\r\\n").Replace("\"", "\\\"")).AppendLine("\");"); + + cscode .Append(" }\r\n") .Append(" return base.").Append(pnv.Name).AppendLine(";") .Append(" }\r\n"); @@ -574,14 +580,14 @@ namespace FreeSql.Internal { if (trycol == null) { nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 没有找到对应的字段,如:{pnv.Name}{findtbrefPkCsName}、{pnv.Name}_{findtbrefPkCsName}"); trytb.AddOrUpdateTableRef(pnv.Name, nvref); - if (isLazy) throw nvref.Exception; + //if (isLazy) throw nvref.Exception; continue; } nvref.Columns.Add(trycol); nvref.RefColumns.Add(tbref.Primarys[a]); - if (isLazy) { + if (isLazy && nvref.Exception == null) { if (a > 0) lmbdWhere.Append(" && "); lmbdWhere.Append("a.").Append(tbref.Primarys[a].CsName).Append(" == this.").Append(trycol.CsName); } @@ -597,9 +603,15 @@ namespace FreeSql.Internal { .Append(" public override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {"); if (vp.Item2) { //get 重写 cscode.Append(" get {\r\n") - .Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {") - .Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propTypeName).Append(">().Where(a => ").Append(lmbdWhere.ToString()).AppendLine(").ToOne();") - .Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;") + .Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {"); + + if (nvref.Exception == null) + cscode.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propTypeName).Append(">().Where(a => ").Append(lmbdWhere.ToString()).AppendLine(").ToOne();") + .Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;"); + else + cscode.Append(" throw new Exception(\"").Append(nvref.Exception.Message.Replace("\r\n", "\\r\\n").Replace("\"", "\\\"")).AppendLine("\");"); + + cscode .Append(" }\r\n") .Append(" return base.").Append(pnv.Name).AppendLine(";") .Append(" }\r\n");