- 优化 导航属性的关系,友好支持 int/int? 映射;

This commit is contained in:
2881099 2019-08-16 20:33:59 +08:00
parent da1d7f782a
commit f80308a6eb
3 changed files with 17 additions and 17 deletions

View File

@ -858,7 +858,7 @@ namespace FreeSql.Tests.Sqlite
public class TestInclude_OneToManyModel2 public class TestInclude_OneToManyModel2
{ {
[Column(IsPrimary = true)] [Column(IsPrimary = true)]
public int model2id { get; set; } public int? model2id { get; set; }
public virtual TestInclude_OneToManyModel1 model1 { get; set; } public virtual TestInclude_OneToManyModel1 model1 { get; set; }
public string m2setting { get; set; } public string m2setting { get; set; }
@ -870,7 +870,7 @@ namespace FreeSql.Tests.Sqlite
[Column(IsIdentity = true)] [Column(IsIdentity = true)]
public int id { get; set; } public int id { get; set; }
public int? model2111Idaaa { get; set; } public int model2111Idaaa { get; set; }
public string title { get; set; } public string title { get; set; }
public List<TestInclude_OneToManyModel4> childs2 { get; set; } public List<TestInclude_OneToManyModel4> childs2 { get; set; }

View File

@ -680,14 +680,14 @@ namespace FreeSql.Internal.CommonProvider
subSelect._where.Clear(); subSelect._where.Clear();
if (tbref.Columns.Count == 1) if (tbref.Columns.Count == 1)
{ {
var arrExp = Expression.NewArrayInit(tbref.Columns[0].CsType, var arrExp = Expression.NewArrayInit(tbref.RefColumns[0].CsType,
list.Select(a => getListValue(a, tbref.Columns[0].CsName, 0)).Distinct() list.Select(a => getListValue(a, tbref.Columns[0].CsName, 0)).Distinct()
.Select(a => Expression.Constant(Convert.ChangeType(a, tbref.Columns[0].CsType))).ToArray()); .Select(a => Expression.Constant(Utils.GetDataReaderValue(tbref.RefColumns[0].CsType, a), tbref.RefColumns[0].CsType)).ToArray());
var otmExpParm1 = Expression.Parameter(typeof(TNavigate), "a"); var otmExpParm1 = Expression.Parameter(typeof(TNavigate), "a");
var containsMethod = _dicTypeMethod.GetOrAdd(tbref.Columns[0].CsType, et => new ConcurrentDictionary<string, MethodInfo>()).GetOrAdd("Contains", mn => var containsMethod = _dicTypeMethod.GetOrAdd(tbref.RefColumns[0].CsType, et => new ConcurrentDictionary<string, MethodInfo>()).GetOrAdd("Contains", mn =>
typeof(Enumerable).GetMethods().Where(a => a.Name == mn).First()).MakeGenericMethod(tbref.Columns[0].CsType); typeof(Enumerable).GetMethods().Where(a => a.Name == mn).First()).MakeGenericMethod(tbref.RefColumns[0].CsType);
var refCol = Expression.MakeMemberAccess(otmExpParm1, tbref2.Properties[tbref.RefColumns[0].CsName]); var refCol = Expression.MakeMemberAccess(otmExpParm1, tbref2.Properties[tbref.RefColumns[0].CsName]);
if (refCol.Type.IsNullableType()) refCol = Expression.Property(refCol, CommonExpression._dicNullableValueProperty.GetOrAdd(refCol.Type, ct1 => ct1.GetProperty("Value"))); //if (refCol.Type.IsNullableType()) refCol = Expression.Property(refCol, CommonExpression._dicNullableValueProperty.GetOrAdd(refCol.Type, ct1 => ct1.GetProperty("Value")));
subSelect.Where(Expression.Lambda<Func<TNavigate, bool>>( subSelect.Where(Expression.Lambda<Func<TNavigate, bool>>(
Expression.Call(null, containsMethod, arrExp, refCol), otmExpParm1)); Expression.Call(null, containsMethod, arrExp, refCol), otmExpParm1));
} }

View File

@ -518,7 +518,7 @@ namespace FreeSql.Internal
{ {
} }
if (trycol != null && trycol.CsType.NullableTypeOrThis() != trytb.Primarys[a].CsType) if (trycol != null && trycol.CsType.NullableTypeOrThis() != trytb.Primarys[a].CsType.NullableTypeOrThis())
{ {
nvref.Exception = new Exception($"【ManyToMany】导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{tbmid.CsName}.{trycol.CsName} 和 {trytb.CsName}.{trytb.Primarys[a].CsName} 类型不一致"); nvref.Exception = new Exception($"【ManyToMany】导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{tbmid.CsName}.{trycol.CsName} 和 {trytb.CsName}.{trytb.Primarys[a].CsName} 类型不一致");
trytb.AddOrUpdateTableRef(pnv.Name, nvref); trytb.AddOrUpdateTableRef(pnv.Name, nvref);
@ -558,7 +558,7 @@ namespace FreeSql.Internal
{ {
} }
if (trycol != null && trycol.CsType.NullableTypeOrThis() != tbref.Primarys[a].CsType) if (trycol != null && trycol.CsType.NullableTypeOrThis() != tbref.Primarys[a].CsType.NullableTypeOrThis())
{ {
nvref.Exception = new Exception($"【ManyToMany】导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{tbmid.CsName}.{trycol.CsName} 和 {tbref.CsName}.{tbref.Primarys[a].CsName} 类型不一致"); nvref.Exception = new Exception($"【ManyToMany】导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{tbmid.CsName}.{trycol.CsName} 和 {tbref.CsName}.{tbref.Primarys[a].CsName} 类型不一致");
trytb.AddOrUpdateTableRef(pnv.Name, nvref); trytb.AddOrUpdateTableRef(pnv.Name, nvref);
@ -653,10 +653,10 @@ namespace FreeSql.Internal
} }
if (trytb.Primarys.Length > 1) if (trytb.Primarys.Length > 1)
{ {
if (trytb.Primarys.Select(a => a.CsType).Distinct().Count() == trytb.Primarys.Length) if (trytb.Primarys.Select(a => a.CsType.NullableTypeOrThis()).Distinct().Count() == trytb.Primarys.Length)
{ {
var pkList = trytb.Primarys.ToList(); var pkList = trytb.Primarys.ToList();
bindColumns.Sort((a, b) => pkList.FindIndex(c => c.CsType == a.CsType.NullableTypeOrThis()).CompareTo(pkList.FindIndex(c => c.CsType == b.CsType.NullableTypeOrThis()))); bindColumns.Sort((a, b) => pkList.FindIndex(c => c.CsType.NullableTypeOrThis() == a.CsType.NullableTypeOrThis()).CompareTo(pkList.FindIndex(c => c.CsType.NullableTypeOrThis() == b.CsType.NullableTypeOrThis())));
} }
else if (string.Compare(string.Join(",", trytb.Primarys.Select(a => a.CsName).OrderBy(a => a)), string.Join(",", bindColumns.Select(a => a.CsName).OrderBy(a => a)), true) == 0) else if (string.Compare(string.Join(",", trytb.Primarys.Select(a => a.CsName).OrderBy(a => a)), string.Join(",", bindColumns.Select(a => a.CsName).OrderBy(a => a)), true) == 0)
{ {
@ -685,7 +685,7 @@ namespace FreeSql.Internal
} }
} }
if (trycol != null && trycol.CsType.NullableTypeOrThis() != trytb.Primarys[a].CsType) if (trycol != null && trycol.CsType.NullableTypeOrThis() != trytb.Primarys[a].CsType.NullableTypeOrThis())
{ {
nvref.Exception = new Exception($"【OneToMany】导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{trytb.CsName}.{trytb.Primarys[a].CsName} 和 {tbref.CsName}.{trycol.CsName} 类型不一致"); nvref.Exception = new Exception($"【OneToMany】导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{trytb.CsName}.{trytb.Primarys[a].CsName} 和 {tbref.CsName}.{trycol.CsName} 类型不一致");
trytb.AddOrUpdateTableRef(pnv.Name, nvref); trytb.AddOrUpdateTableRef(pnv.Name, nvref);
@ -776,7 +776,7 @@ namespace FreeSql.Internal
var isOnoToOne = pnv.PropertyType != trytb.Type && var isOnoToOne = pnv.PropertyType != trytb.Type &&
tbref.Properties.Where(z => z.Value.PropertyType == trytb.Type).Any() && tbref.Properties.Where(z => z.Value.PropertyType == trytb.Type).Any() &&
tbref.Primarys.Length == trytb.Primarys.Length && tbref.Primarys.Length == trytb.Primarys.Length &&
string.Join(",", tbref.Primarys.Select(a => a.CsType.FullName).OrderBy(a => a)) == string.Join(",", trytb.Primarys.Select(a => a.CsType.FullName).OrderBy(a => a)); string.Join(",", tbref.Primarys.Select(a => a.CsType.NullableTypeOrThis().FullName).OrderBy(a => a)) == string.Join(",", trytb.Primarys.Select(a => a.CsType.NullableTypeOrThis().FullName).OrderBy(a => a));
List<ColumnInfo> bindColumns = new List<ColumnInfo>(); List<ColumnInfo> bindColumns = new List<ColumnInfo>();
if (pnvBind != null) if (pnvBind != null)
@ -803,10 +803,10 @@ namespace FreeSql.Internal
} }
if (tbref.Primarys.Length > 1) if (tbref.Primarys.Length > 1)
{ {
if (tbref.Primarys.Select(a => a.CsType).Distinct().Count() == tbref.Primarys.Length) if (tbref.Primarys.Select(a => a.CsType.NullableTypeOrThis()).Distinct().Count() == tbref.Primarys.Length)
{ {
var pkList = tbref.Primarys.ToList(); var pkList = tbref.Primarys.ToList();
bindColumns.Sort((a, b) => pkList.FindIndex(c => c.CsType == a.CsType.NullableTypeOrThis()).CompareTo(pkList.FindIndex(c => c.CsType == b.CsType.NullableTypeOrThis()))); bindColumns.Sort((a, b) => pkList.FindIndex(c => c.CsType.NullableTypeOrThis() == a.CsType.NullableTypeOrThis()).CompareTo(pkList.FindIndex(c => c.CsType.NullableTypeOrThis() == b.CsType.NullableTypeOrThis())));
} }
else if (string.Compare(string.Join(",", tbref.Primarys.Select(a => a.CsName).OrderBy(a => a)), string.Join(",", bindColumns.Select(a => a.CsName).OrderBy(a => a)), true) == 0) else if (string.Compare(string.Join(",", tbref.Primarys.Select(a => a.CsName).OrderBy(a => a)), string.Join(",", bindColumns.Select(a => a.CsName).OrderBy(a => a)), true) == 0)
{ {
@ -831,7 +831,7 @@ namespace FreeSql.Internal
//一对一,主键与主键查找 //一对一,主键与主键查找
if (isOnoToOne) if (isOnoToOne)
{ {
var trytbpks = trytb.Primarys.Where(z => z.CsType == tbref.Primarys[a].CsType); //一对一,按类型 var trytbpks = trytb.Primarys.Where(z => z.CsType.NullableTypeOrThis() == tbref.Primarys[a].CsType.NullableTypeOrThis()); //一对一,按类型
if (trytbpks.Count() == 1) trycol = trytbpks.First(); if (trytbpks.Count() == 1) trycol = trytbpks.First();
else else
{ {
@ -850,7 +850,7 @@ namespace FreeSql.Internal
} }
} }
} }
if (trycol != null && trycol.CsType.NullableTypeOrThis() != tbref.Primarys[a].CsType) if (trycol != null && trycol.CsType.NullableTypeOrThis() != tbref.Primarys[a].CsType.NullableTypeOrThis())
{ {
nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{trytb.CsName}.{trycol.CsName} 和 {tbref.CsName}.{tbref.Primarys[a].CsName} 类型不一致"); nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 解析错误,{trytb.CsName}.{trycol.CsName} 和 {tbref.CsName}.{tbref.Primarys[a].CsName} 类型不一致");
trytb.AddOrUpdateTableRef(pnv.Name, nvref); trytb.AddOrUpdateTableRef(pnv.Name, nvref);