Skip to content

Commit 5a69128

Browse files
2881028810
authored andcommitted
- 优化 延时导航属性的错误提醒,当无法匹配错误,转到重写类 get 时抛出(实现延时导航属性,与普通导航一起使用);
1 parent 98d1201 commit 5a69128

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

FreeSql/FreeSql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>0.5.21</Version>
5+
<Version>0.5.22</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>

FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,8 @@ protected GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2() {
704704
Expression.Add(dataIndexExp, Expression.Constant(1))
705705
);
706706
else {
707-
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) });
707+
var propLazyType = _commonUtils.GetTableByEntity(prop.PropertyType)?.TypeLazy ?? prop.PropertyType;
708+
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) });
708709
}
709710
}
710711
blockExp.AddRange(new Expression[] {

FreeSql/Internal/UtilsExpressionTree.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -467,21 +467,21 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
467467
if (trycol != null && trycol.CsType.NullableTypeOrThis() != trytb.Primarys[a].CsType) {
468468
nvref.Exception = new Exception($"【OneToMany】导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{trytb.CsName}.{trytb.Primarys[a].CsName}{tbref.CsName}.{trycol.CsName} 类型不一致");
469469
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
470-
if (isLazy) throw nvref.Exception;
470+
//if (isLazy) throw nvref.Exception;
471471
continue;
472472
}
473473
if (trycol == null) {
474474
nvref.Exception = new Exception($"【OneToMany】导航属性 {trytbTypeName}.{pnv.Name}{tbref.CsName} 中没有找到对应的字段,如:{findtrytb}{findtrytbPkCsName}{findtrytb}_{findtrytbPkCsName}" + (refprop == null ? "" : $"、{refprop.Name}{findtrytbPkCsName}{refprop.Name}_{findtrytbPkCsName}"));
475475
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
476-
if (isLazy) throw nvref.Exception;
476+
//if (isLazy) throw nvref.Exception;
477477
continue;
478478
}
479479
}
480480

481481
nvref.Columns.Add(trytb.Primarys[a]);
482482
nvref.RefColumns.Add(trycol);
483483

484-
if (isLazy) {
484+
if (isLazy && nvref.Exception == null) {
485485
if (a > 0) lmbdWhere.Append(" && ");
486486
lmbdWhere.Append("a.").Append(trycol.CsName).Append(" == this.").Append(trytb.Primarys[a].CsName);
487487

@@ -506,13 +506,19 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
506506
.Append(" public override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {");
507507
if (vp.Item2) { //get 重写
508508
cscode.Append(" get {\r\n")
509-
.Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {")
510-
.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();");
511-
if (refprop != null) {
512-
cscode.Append(" foreach (var loc1 in base.").Append(pnv.Name).AppendLine(")")
513-
.Append(" loc1.").Append(refprop.Name).AppendLine(" = this;");
514-
}
515-
cscode.Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;")
509+
.Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {");
510+
511+
if (nvref.Exception == null) {
512+
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();");
513+
if (refprop != null) {
514+
cscode.Append(" foreach (var loc1 in base.").Append(pnv.Name).AppendLine(")")
515+
.Append(" loc1.").Append(refprop.Name).AppendLine(" = this;")
516+
.Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;");
517+
}
518+
} else
519+
cscode.Append(" throw new Exception(\"").Append(nvref.Exception.Message.Replace("\r\n", "\\r\\n").Replace("\"", "\\\"")).AppendLine("\");");
520+
521+
cscode
516522
.Append(" }\r\n")
517523
.Append(" return base.").Append(pnv.Name).AppendLine(";")
518524
.Append(" }\r\n");
@@ -574,14 +580,14 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
574580
if (trycol == null) {
575581
nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 没有找到对应的字段,如:{pnv.Name}{findtbrefPkCsName}{pnv.Name}_{findtbrefPkCsName}");
576582
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
577-
if (isLazy) throw nvref.Exception;
583+
//if (isLazy) throw nvref.Exception;
578584
continue;
579585
}
580586

581587
nvref.Columns.Add(trycol);
582588
nvref.RefColumns.Add(tbref.Primarys[a]);
583589

584-
if (isLazy) {
590+
if (isLazy && nvref.Exception == null) {
585591
if (a > 0) lmbdWhere.Append(" && ");
586592
lmbdWhere.Append("a.").Append(tbref.Primarys[a].CsName).Append(" == this.").Append(trycol.CsName);
587593
}
@@ -597,9 +603,15 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
597603
.Append(" public override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {");
598604
if (vp.Item2) { //get 重写
599605
cscode.Append(" get {\r\n")
600-
.Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {")
601-
.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propTypeName).Append(">().Where(a => ").Append(lmbdWhere.ToString()).AppendLine(").ToOne();")
602-
.Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;")
606+
.Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {");
607+
608+
if (nvref.Exception == null)
609+
cscode.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propTypeName).Append(">().Where(a => ").Append(lmbdWhere.ToString()).AppendLine(").ToOne();")
610+
.Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;");
611+
else
612+
cscode.Append(" throw new Exception(\"").Append(nvref.Exception.Message.Replace("\r\n", "\\r\\n").Replace("\"", "\\\"")).AppendLine("\");");
613+
614+
cscode
603615
.Append(" }\r\n")
604616
.Append(" return base.").Append(pnv.Name).AppendLine(";")
605617
.Append(" }\r\n");

0 commit comments

Comments
 (0)