mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-22 12:09:29 +08:00
Update ComplexModel
This commit is contained in:
@@ -12,7 +12,7 @@ namespace OrmTest.Demo
|
|||||||
public static void Init()
|
public static void Init()
|
||||||
{
|
{
|
||||||
var db = GetInstance();
|
var db = GetInstance();
|
||||||
var student = db.Queryable<CMStudent>().ToList();
|
var students = db.Queryable<CMStudent>().ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,12 +23,24 @@ namespace OrmTest.Demo
|
|||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public int SchoolId { 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)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
public CMSchool SchoolSingle
|
public CMSchool SchoolSingle
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return base.CreateMapping<CMSchool>().Single(it => it.Id == this.Id);
|
return base.CreateMapping<CMSchool>().Single(it => it.Id == this.SchoolId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +49,7 @@ namespace OrmTest.Demo
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return base.CreateMapping<CMSchool>().Where(it => it.Id == this.Id).ToList();
|
return base.CreateMapping<CMSchool>().Where(it => it.Id == this.SchoolId).ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,31 +17,31 @@ namespace OrmTest
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
/***Unit Test***/
|
///***Unit Test***/
|
||||||
new Field(1).Init();
|
//new Field(1).Init();
|
||||||
new Where(1).Init();
|
//new Where(1).Init();
|
||||||
new Method(1).Init();
|
//new Method(1).Init();
|
||||||
new JoinQuery(1).Init();
|
//new JoinQuery(1).Init();
|
||||||
new SingleQuery(1).Init();
|
//new SingleQuery(1).Init();
|
||||||
new SelectQuery(1).Init();
|
//new SelectQuery(1).Init();
|
||||||
new AutoClose(1).Init();
|
//new AutoClose(1).Init();
|
||||||
new Insert(1).Init();
|
//new Insert(1).Init();
|
||||||
new Delete(1).Init();
|
//new Delete(1).Init();
|
||||||
new Update(1).Init();
|
//new Update(1).Init();
|
||||||
new Mapping(1).Init();
|
//new Mapping(1).Init();
|
||||||
new DataTest(1).Init();
|
//new DataTest(1).Init();
|
||||||
/***Performance Test***/
|
///***Performance Test***/
|
||||||
new SqlSugarPerformance(100).Select();
|
//new SqlSugarPerformance(100).Select();
|
||||||
|
|
||||||
/***Demo***/
|
///***Demo***/
|
||||||
OrmTest.Demo.Query.Init();
|
//OrmTest.Demo.Query.Init();
|
||||||
OrmTest.Demo.Insert.Init();
|
//OrmTest.Demo.Insert.Init();
|
||||||
OrmTest.Demo.Delete.Init();
|
//OrmTest.Demo.Delete.Init();
|
||||||
OrmTest.Demo.Update.Init();
|
//OrmTest.Demo.Update.Init();
|
||||||
OrmTest.Demo.DbFirst.Init();
|
//OrmTest.Demo.DbFirst.Init();
|
||||||
OrmTest.Demo.JoinSql.Init();
|
//OrmTest.Demo.JoinSql.Init();
|
||||||
OrmTest.Demo.Filter.Init();
|
//OrmTest.Demo.Filter.Init();
|
||||||
OrmTest.Demo.MaterSlave.Init();
|
//OrmTest.Demo.MaterSlave.Init();
|
||||||
OrmTest.Demo.ComplexModel.Init();
|
OrmTest.Demo.ComplexModel.Init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -504,20 +504,39 @@ namespace SqlSugar
|
|||||||
List<TResult> result = null;
|
List<TResult> result = null;
|
||||||
var sqlObj = this.ToSql();
|
var sqlObj = this.ToSql();
|
||||||
var isComplexModel = QueryBuilder.IsComplexModel(sqlObj.Key);
|
var isComplexModel = QueryBuilder.IsComplexModel(sqlObj.Key);
|
||||||
|
var entityType = typeof(TResult);
|
||||||
using (var dataReader = this.Db.GetDataReader(sqlObj.Key, sqlObj.Value.ToArray()))
|
using (var dataReader = this.Db.GetDataReader(sqlObj.Key, sqlObj.Value.ToArray()))
|
||||||
{
|
{
|
||||||
var tType = typeof(TResult);
|
if (entityType.IsAnonymousType() || isComplexModel)
|
||||||
if (tType.IsAnonymousType() || isComplexModel)
|
|
||||||
{
|
{
|
||||||
result = this.Context.RewritableMethods.DataReaderToDynamicList<TResult>(dataReader);
|
result = this.Context.RewritableMethods.DataReaderToDynamicList<TResult>(dataReader);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = this.Bind.DataReaderToList<TResult>(tType, dataReader, QueryBuilder.SelectCacheKey);
|
result = this.Bind.DataReaderToList<TResult>(entityType, dataReader, QueryBuilder.SelectCacheKey);
|
||||||
}
|
}
|
||||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection) this.Context.Close();
|
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection) this.Context.Close();
|
||||||
}
|
}
|
||||||
RestoreMapping();
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
protected List<string> GetPrimaryKeys()
|
protected List<string> GetPrimaryKeys()
|
||||||
|
@@ -17,7 +17,8 @@ namespace SqlSugar
|
|||||||
internal static Type ByteArrayType = typeof(byte[]);
|
internal static Type ByteArrayType = typeof(byte[]);
|
||||||
internal static Type BoolType = typeof(bool);
|
internal static Type BoolType = typeof(bool);
|
||||||
internal static Type ObjType = typeof(object);
|
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<string, string>);
|
internal static Type DicSS = typeof(KeyValuePair<string, string>);
|
||||||
internal static Type DicSi = typeof(KeyValuePair<string, int>);
|
internal static Type DicSi = typeof(KeyValuePair<string, int>);
|
||||||
internal static Type Dicii = typeof(KeyValuePair<int, int>);
|
internal static Type Dicii = typeof(KeyValuePair<int, int>);
|
||||||
|
@@ -89,7 +89,7 @@ namespace SqlSugar
|
|||||||
var addValue = readerValues[name];
|
var addValue = readerValues[name];
|
||||||
if (addValue == DBNull.Value)
|
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;
|
addValue = 0;
|
||||||
}
|
}
|
||||||
|
@@ -7,10 +7,14 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
public class ModelContext
|
public class ModelContext
|
||||||
{
|
{
|
||||||
internal SqlSugarClient Context { get; set; }
|
[SugarColumn(IsIgnore = true)]
|
||||||
public ISugarQueryable<T> CreateMapping<T>()where T:class,new()
|
public SqlSugarClient Context { get; set; }
|
||||||
|
public ISugarQueryable<T> CreateMapping<T>() where T : class, new()
|
||||||
|
{
|
||||||
|
using (Context)
|
||||||
{
|
{
|
||||||
return Context.Queryable<T>();
|
return Context.Queryable<T>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -98,6 +98,7 @@ namespace SqlSugar
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual ISugarQueryable<T> Queryable<T>() where T : class, new()
|
public virtual ISugarQueryable<T> Queryable<T>() where T : class, new()
|
||||||
{
|
{
|
||||||
|
|
||||||
InitMppingInfo<T>();
|
InitMppingInfo<T>();
|
||||||
var result = base.CreateQueryable<T>();
|
var result = base.CreateQueryable<T>();
|
||||||
return result;
|
return result;
|
||||||
|
Reference in New Issue
Block a user