diff --git a/SqlServerTest/Demos/ComplexModel.cs b/SqlServerTest/Demos/ComplexModel.cs index 9500de3ef..905786bc3 100644 --- a/SqlServerTest/Demos/ComplexModel.cs +++ b/SqlServerTest/Demos/ComplexModel.cs @@ -12,7 +12,7 @@ namespace OrmTest.Demo public static void Init() { var db = GetInstance(); - var student = db.Queryable().ToList(); + var students = db.Queryable().ToList(); } } @@ -23,12 +23,24 @@ namespace OrmTest.Demo public string Name { get; set; } public int SchoolId { get; set; } + [SugarColumn(IsIgnore = true)] + public string SchoolName + { + get + { + if (this.SchoolSingle != null) + return this.SchoolSingle.Name; + else + return null; + } + } + [SugarColumn(IsIgnore = true)] public CMSchool SchoolSingle { get { - return base.CreateMapping().Single(it => it.Id == this.Id); + return base.CreateMapping().Single(it => it.Id == this.SchoolId); } } @@ -37,7 +49,7 @@ namespace OrmTest.Demo { get { - return base.CreateMapping().Where(it => it.Id == this.Id).ToList(); + return base.CreateMapping().Where(it => it.Id == this.SchoolId).ToList(); } } } diff --git a/SqlServerTest/Program.cs b/SqlServerTest/Program.cs index caafd709e..060854168 100644 --- a/SqlServerTest/Program.cs +++ b/SqlServerTest/Program.cs @@ -17,31 +17,31 @@ namespace OrmTest { static void Main(string[] args) { - /***Unit Test***/ - new Field(1).Init(); - new Where(1).Init(); - new Method(1).Init(); - new JoinQuery(1).Init(); - new SingleQuery(1).Init(); - new SelectQuery(1).Init(); - new AutoClose(1).Init(); - new Insert(1).Init(); - new Delete(1).Init(); - new Update(1).Init(); - new Mapping(1).Init(); - new DataTest(1).Init(); - /***Performance Test***/ - new SqlSugarPerformance(100).Select(); + ///***Unit Test***/ + //new Field(1).Init(); + //new Where(1).Init(); + //new Method(1).Init(); + //new JoinQuery(1).Init(); + //new SingleQuery(1).Init(); + //new SelectQuery(1).Init(); + //new AutoClose(1).Init(); + //new Insert(1).Init(); + //new Delete(1).Init(); + //new Update(1).Init(); + //new Mapping(1).Init(); + //new DataTest(1).Init(); + ///***Performance Test***/ + //new SqlSugarPerformance(100).Select(); - /***Demo***/ - OrmTest.Demo.Query.Init(); - OrmTest.Demo.Insert.Init(); - OrmTest.Demo.Delete.Init(); - OrmTest.Demo.Update.Init(); - OrmTest.Demo.DbFirst.Init(); - OrmTest.Demo.JoinSql.Init(); - OrmTest.Demo.Filter.Init(); - OrmTest.Demo.MaterSlave.Init(); + ///***Demo***/ + //OrmTest.Demo.Query.Init(); + //OrmTest.Demo.Insert.Init(); + //OrmTest.Demo.Delete.Init(); + //OrmTest.Demo.Update.Init(); + //OrmTest.Demo.DbFirst.Init(); + //OrmTest.Demo.JoinSql.Init(); + //OrmTest.Demo.Filter.Init(); + //OrmTest.Demo.MaterSlave.Init(); OrmTest.Demo.ComplexModel.Init(); } } diff --git a/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index a5f2e43dd..cf9916d27 100644 --- a/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -504,20 +504,39 @@ namespace SqlSugar List result = null; var sqlObj = this.ToSql(); var isComplexModel = QueryBuilder.IsComplexModel(sqlObj.Key); + var entityType = typeof(TResult); using (var dataReader = this.Db.GetDataReader(sqlObj.Key, sqlObj.Value.ToArray())) { - var tType = typeof(TResult); - if (tType.IsAnonymousType() || isComplexModel) + if (entityType.IsAnonymousType() || isComplexModel) { result = this.Context.RewritableMethods.DataReaderToDynamicList(dataReader); } else { - result = this.Bind.DataReaderToList(tType, dataReader, QueryBuilder.SelectCacheKey); + result = this.Bind.DataReaderToList(entityType, dataReader, QueryBuilder.SelectCacheKey); } if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection) this.Context.Close(); } RestoreMapping(); + if (result.IsValuable()) + { + if (entityType.BaseType.IsValuable() && entityType.BaseType == PubConst.ModelType) + { + foreach (var item in result) + { + var contextProperty=item.GetType().GetProperty("Context"); + ConnectionConfig config = new ConnectionConfig(); + config =this.Context.CurrentConnectionConfig; + var newClient = new SqlSugarClient(config); + newClient.MappingColumns = this.Context.MappingColumns; + newClient.MappingTables = this.Context.MappingTables; + newClient.IgnoreColumns = this.Context.IgnoreColumns; + newClient.Ado.MasterConnectionConfig = this.Context.Ado.MasterConnectionConfig; + newClient.Ado.SlaveConnectionConfigs = this.Context.Ado.SlaveConnectionConfigs; + contextProperty.SetValue(item, newClient, null); + } + } + } return result; } protected List GetPrimaryKeys() diff --git a/SqlSugar/Common/PubConst.cs b/SqlSugar/Common/PubConst.cs index 3980de698..523a0b2f8 100644 --- a/SqlSugar/Common/PubConst.cs +++ b/SqlSugar/Common/PubConst.cs @@ -17,7 +17,8 @@ namespace SqlSugar internal static Type ByteArrayType = typeof(byte[]); internal static Type BoolType = typeof(bool); internal static Type ObjType = typeof(object); - internal static Type Dob = typeof(double); + internal static Type DobType = typeof(double); + internal static Type ModelType= typeof(ModelContext); internal static Type DicSS = typeof(KeyValuePair); internal static Type DicSi = typeof(KeyValuePair); internal static Type Dicii = typeof(KeyValuePair); diff --git a/SqlSugar/Common/RewritableMethods.cs b/SqlSugar/Common/RewritableMethods.cs index fff2e0d92..0a359ac3d 100644 --- a/SqlSugar/Common/RewritableMethods.cs +++ b/SqlSugar/Common/RewritableMethods.cs @@ -89,7 +89,7 @@ namespace SqlSugar var addValue = readerValues[name]; if (addValue == DBNull.Value) { - if (item.PropertyType.IsIn(PubConst.IntType, PubConst.DecType, PubConst.Dob, PubConst.ByteType)) + if (item.PropertyType.IsIn(PubConst.IntType, PubConst.DecType, PubConst.DobType, PubConst.ByteType)) { addValue = 0; } diff --git a/SqlSugar/Entities/ModelContext.cs b/SqlSugar/Entities/ModelContext.cs index dfa6d6271..32ec11834 100644 --- a/SqlSugar/Entities/ModelContext.cs +++ b/SqlSugar/Entities/ModelContext.cs @@ -7,10 +7,14 @@ namespace SqlSugar { public class ModelContext { - internal SqlSugarClient Context { get; set; } - public ISugarQueryable CreateMapping()where T:class,new() + [SugarColumn(IsIgnore = true)] + public SqlSugarClient Context { get; set; } + public ISugarQueryable CreateMapping() where T : class, new() { - return Context.Queryable(); + using (Context) + { + return Context.Queryable(); + } } } } diff --git a/SqlSugar/SqlSugarClient.cs b/SqlSugar/SqlSugarClient.cs index 257393fc6..391bdc26b 100644 --- a/SqlSugar/SqlSugarClient.cs +++ b/SqlSugar/SqlSugarClient.cs @@ -98,6 +98,7 @@ namespace SqlSugar /// public virtual ISugarQueryable Queryable() where T : class, new() { + InitMppingInfo(); var result = base.CreateQueryable(); return result;