From 67358ab9ab40e003e909546ca7c4d296fd61d393 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 3 May 2018 16:22:18 +0800 Subject: [PATCH] ModelContext support inheritance --- Src/Asp.Net/SqlServerTest/Demos/6_ComplexModel.cs | 1 + .../Abstract/QueryableProvider/QueryableProvider.cs | 2 +- Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Src/Asp.Net/SqlServerTest/Demos/6_ComplexModel.cs b/Src/Asp.Net/SqlServerTest/Demos/6_ComplexModel.cs index 7da92b5c0..b5eecd86c 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/6_ComplexModel.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/6_ComplexModel.cs @@ -12,6 +12,7 @@ namespace OrmTest.Demo public static void Init() { var db = GetInstance(); + db.Insertable(new CMStudent() { SchoolId = 1, Name = "xx1" }).ExecuteCommand(); var students = db.Queryable().ToList(); if (students != null) { diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 8a94a2691..450f65c02 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1180,7 +1180,7 @@ namespace SqlSugar { if (result.HasValue()) { - if (entityType.GetTypeInfo().BaseType.HasValue() && entityType.GetTypeInfo().BaseType == UtilConstants.ModelType) + if (UtilMethods.GetRootBaseType(entityType).HasValue() &&UtilMethods.GetRootBaseType(entityType) == UtilConstants.ModelType) { foreach (var item in result) { diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs index 8caf4b847..410d6dd9a 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs @@ -17,6 +17,17 @@ namespace SqlSugar return type==null ? oldType : type; } + internal static Type GetRootBaseType(Type entityType) + { + var baseType = entityType.BaseType; + while (baseType != null && baseType.BaseType != UtilConstants.ObjType) + { + baseType = baseType.BaseType; + } + return baseType; + } + + internal static Type GetUnderType(PropertyInfo propertyInfo, ref bool isNullable) { Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);