Browse Source

Update ComplexModel

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
2b46386b98
  1. 18
      SqlServerTest/Demos/ComplexModel.cs
  2. 48
      SqlServerTest/Program.cs
  3. 25
      SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  4. 3
      SqlSugar/Common/PubConst.cs
  5. 2
      SqlSugar/Common/RewritableMethods.cs
  6. 10
      SqlSugar/Entities/ModelContext.cs
  7. 1
      SqlSugar/SqlSugarClient.cs

18
SqlServerTest/Demos/ComplexModel.cs

@ -12,7 +12,7 @@ namespace OrmTest.Demo
public static void Init()
{
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 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<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
{
return base.CreateMapping<CMSchool>().Where(it => it.Id == this.Id).ToList();
return base.CreateMapping<CMSchool>().Where(it => it.Id == this.SchoolId).ToList();
}
}
}

48
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();
}
}

25
SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -504,20 +504,39 @@ namespace SqlSugar
List<TResult> 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<TResult>(dataReader);
}
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();
}
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<string> GetPrimaryKeys()

3
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<string, string>);
internal static Type DicSi = typeof(KeyValuePair<string, int>);
internal static Type Dicii = typeof(KeyValuePair<int, int>);

2
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;
}

10
SqlSugar/Entities/ModelContext.cs

@ -7,10 +7,14 @@ namespace SqlSugar
{
public class ModelContext
{
internal SqlSugarClient Context { get; set; }
public ISugarQueryable<T> CreateMapping<T>()where T:class,new()
[SugarColumn(IsIgnore = true)]
public SqlSugarClient Context { get; set; }
public ISugarQueryable<T> CreateMapping<T>() where T : class, new()
{
return Context.Queryable<T>();
using (Context)
{
return Context.Queryable<T>();
}
}
}
}

1
SqlSugar/SqlSugarClient.cs

@ -98,6 +98,7 @@ namespace SqlSugar
/// </summary>
public virtual ISugarQueryable<T> Queryable<T>() where T : class, new()
{
InitMppingInfo<T>();
var result = base.CreateQueryable<T>();
return result;

Loading…
Cancel
Save