mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-01 19:03:58 +08:00
-
This commit is contained in:
@@ -39,7 +39,7 @@ namespace OrmTest.UnitTest
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new AttributeConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true, EntityNamespace= "OrmTest.Models" });
|
||||
SqlSugarClient db = new SqlSugarClient(new AttributeConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ namespace SqlSugar
|
||||
fileName = mappInfo.DbColumnName;
|
||||
}
|
||||
}
|
||||
if (Context.IgnoreColumns != null && Context.IgnoreColumns.Any(it => it.EntityPropertyName.Equals(propertyInfo.Name, StringComparison.CurrentCultureIgnoreCase)
|
||||
if (Context.IgnoreColumns != null && Context.IgnoreColumns.Any(it => it.PropertyName.Equals(propertyInfo.Name, StringComparison.CurrentCultureIgnoreCase)
|
||||
&& it.EntityName.Equals(type.Name, StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
continue;
|
||||
|
||||
@@ -289,7 +289,7 @@ namespace SqlSugar
|
||||
{
|
||||
pre = Builder.GetTranslationColumnName(TableShortName) + ".";
|
||||
}
|
||||
reval = string.Join(",", this.Context.EntityProvider.GetEntityInfo(this.EntityType).Columns.Where(it=>!it.IsIgnore).Select(it => pre + Builder.GetTranslationColumnName(it.EnitytName,it.PropertyName)));
|
||||
reval = string.Join(",", this.Context.EntityProvider.GetEntityInfo(this.EntityType).Columns.Where(it=>!it.IsIgnore).Select(it => pre + Builder.GetTranslationColumnName(it.EntityName,it.PropertyName)));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -11,26 +11,6 @@ namespace SqlSugar
|
||||
{
|
||||
public SqlSugarClient Context { get; set; }
|
||||
|
||||
public List<EntityInfo> GetAllEntities()
|
||||
{
|
||||
string cacheKey = "GetAllEntities";
|
||||
return CacheFactory.Func<List<EntityInfo>>(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
return cm[key];
|
||||
|
||||
}, (cm, key) =>
|
||||
{
|
||||
List<EntityInfo> reval = new List<EntityInfo>();
|
||||
var classes = Assembly.Load(this.Context.EntityNamespace.Split('.').First()).GetTypes();
|
||||
foreach (var item in classes)
|
||||
{
|
||||
if (item.FullName.Contains(this.Context.EntityNamespace))
|
||||
reval.Add(GetEntityInfo(item));
|
||||
}
|
||||
return reval;
|
||||
});
|
||||
}
|
||||
public EntityInfo GetEntityInfo<T>()
|
||||
{
|
||||
return GetEntityInfo(typeof(T));
|
||||
@@ -112,7 +92,7 @@ namespace SqlSugar
|
||||
.Select(it => (SugarColumn)it)
|
||||
.FirstOrDefault();
|
||||
column.DbTableName = result.DbTableName;
|
||||
column.EnitytName = result.EntityName;
|
||||
column.EntityName = result.EntityName;
|
||||
column.PropertyName = property.Name;
|
||||
column.PropertyInfo = property;
|
||||
if (sugarColumn.IsNullOrEmpty())
|
||||
|
||||
@@ -123,7 +123,7 @@ namespace SqlSugar
|
||||
var currentIgnoreColumns = this.Context.IgnoreColumns.Where(it => it.EntityName == this.EntityInfo.EntityName).ToList();
|
||||
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it =>
|
||||
{
|
||||
return !currentIgnoreColumns.Any(i => it.PropertyName.Equals(i.EntityPropertyName, StringComparison.CurrentCulture));
|
||||
return !currentIgnoreColumns.Any(i => it.PropertyName.Equals(i.PropertyName, StringComparison.CurrentCulture));
|
||||
}).ToList();
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -162,7 +162,7 @@ namespace SqlSugar
|
||||
var currentIgnoreColumns = this.Context.IgnoreColumns.Where(it => it.EntityName == this.EntityInfo.EntityName).ToList();
|
||||
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it =>
|
||||
{
|
||||
return !currentIgnoreColumns.Any(i => it.PropertyName.Equals(i.EntityPropertyName, StringComparison.CurrentCulture));
|
||||
return !currentIgnoreColumns.Any(i => it.PropertyName.Equals(i.PropertyName, StringComparison.CurrentCulture));
|
||||
}).ToList();
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -14,7 +14,6 @@ namespace SqlSugar
|
||||
|
||||
public class AttributeConfig : IConnectionConfig
|
||||
{
|
||||
public string EntityNamespace { get; set; }
|
||||
public string DbType { get; set; }
|
||||
public string ConnectionString { get; set; }
|
||||
public bool IsAutoCloseConnection { get; set; }
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace SqlSugar
|
||||
public bool IsNullable { get; set; }
|
||||
public bool IsIdentity { get; set; }
|
||||
public bool IsPrimarykey { get; set; }
|
||||
public string EnitytName { get; set; }
|
||||
public string EntityName { get; set; }
|
||||
public string DbTableName { get; set; }
|
||||
public bool IsIgnore { get; set; }
|
||||
}
|
||||
|
||||
@@ -9,6 +9,6 @@ namespace SqlSugar
|
||||
public class IgnoreComumn
|
||||
{
|
||||
public string EntityName { get; set; }
|
||||
public string EntityPropertyName { get; set; }
|
||||
public string PropertyName { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,8 +27,8 @@ namespace SqlSugar
|
||||
{
|
||||
public void Add(string propertyName, string EntityName)
|
||||
{
|
||||
this.RemoveAll(it =>it.EntityName==EntityName&&it.EntityPropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
this.Add(new IgnoreComumn() { EntityPropertyName = propertyName, EntityName = EntityName });
|
||||
this.RemoveAll(it =>it.EntityName==EntityName&&it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
this.Add(new IgnoreComumn() { PropertyName = propertyName, EntityName = EntityName });
|
||||
}
|
||||
|
||||
public new void Clear()
|
||||
|
||||
@@ -153,7 +153,7 @@ namespace SqlSugar
|
||||
{
|
||||
if (this.Context.IgnoreComumnList != null
|
||||
&& this.Context.IgnoreComumnList.Any(
|
||||
it => it.EntityName == item.Type.Name && it.EntityPropertyName == property.Name))
|
||||
it => it.EntityName == item.Type.Name && it.PropertyName == property.Name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ namespace SqlSugar
|
||||
{
|
||||
if (this.Context.IgnoreComumnList != null
|
||||
&& this.Context.IgnoreComumnList.Any(
|
||||
it => it.EntityName == item.Type.Name && it.EntityPropertyName == property.Name))
|
||||
it => it.EntityName == item.Type.Name && it.PropertyName == property.Name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -27,48 +27,65 @@ namespace SqlSugar
|
||||
protected ICodeFirst _CodeFirst;
|
||||
protected IDbMaintenance _DbMaintenance;
|
||||
|
||||
protected void InitConstructor()
|
||||
|
||||
protected void InitMppingInfo<T, T2, T3, T4>()
|
||||
{
|
||||
this.ContextID = Guid.NewGuid();
|
||||
if (this.CurrentConnectionConfig is AttributeConfig)
|
||||
InitMppingInfo<T, T2, T3>();
|
||||
InitMppingInfo<T4>();
|
||||
}
|
||||
|
||||
protected void InitMppingInfo<T, T2, T3>()
|
||||
{
|
||||
InitMppingInfo<T, T2>();
|
||||
InitMppingInfo<T3>();
|
||||
}
|
||||
|
||||
protected void InitMppingInfo<T, T2>()
|
||||
{
|
||||
InitMppingInfo<T>();
|
||||
InitMppingInfo<T2>();
|
||||
}
|
||||
|
||||
protected void InitMppingInfo<T>()
|
||||
{
|
||||
string cacheKey = "Context.InitAttributeMappingTables";
|
||||
CacheFactory.Action<EntityInfo>(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
var cacheInfo = cm[key];
|
||||
InitMppingInfo(cacheInfo);
|
||||
},
|
||||
(cm, key) =>
|
||||
{
|
||||
var reval = this.Context.EntityProvider.GetEntityInfo<T>();
|
||||
InitMppingInfo(reval);
|
||||
return reval;
|
||||
});
|
||||
}
|
||||
private void InitMppingInfo(EntityInfo entityInfo)
|
||||
{
|
||||
if (!this.MappingTables.Any(it => it.EntityName == entityInfo.EntityName))
|
||||
{
|
||||
string cacheKey = "Context.InitAttributeMappingTables";
|
||||
CacheFactory.Action<Tuple<MappingTableList, MappingColumnList, IgnoreComumnList>>(cacheKey,
|
||||
(cm, key) =>
|
||||
if (entityInfo.DbTableName != entityInfo.EntityName)
|
||||
{
|
||||
var cacheInfo = cm[key];
|
||||
this.MappingTables = cacheInfo.Item1;
|
||||
this.MappingColumns = cacheInfo.Item2;
|
||||
this.IgnoreColumns = cacheInfo.Item3;
|
||||
},
|
||||
(cm, key) =>
|
||||
{
|
||||
var entities = this.Context.EntityProvider.GetAllEntities();
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
if (entity.Type.IsAnonymousType()) continue;
|
||||
if (entity.DbTableName!=null&&!entity.DbTableName.Equals(entity.EntityName))
|
||||
{
|
||||
this.MappingTables.Add(entity.EntityName, entity.DbTableName);
|
||||
}
|
||||
foreach (var column in entity.Columns)
|
||||
{
|
||||
if (column.IsIgnore)
|
||||
{
|
||||
this.IgnoreColumns.Add(column.PropertyName, column.EnitytName);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!column.DbColumnName.Equals(column.PropertyName))
|
||||
{
|
||||
this.MappingColumns.Add(column.PropertyName, column.DbColumnName, column.EnitytName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var result= Tuple.Create<MappingTableList, MappingColumnList, IgnoreComumnList>(this.MappingTables,this.MappingColumns,this.IgnoreColumns);
|
||||
return this.Context.RewritableMethods.TranslateCopy(result);
|
||||
});
|
||||
this.MappingTables.Add(entityInfo.EntityName, entityInfo.DbTableName);
|
||||
}
|
||||
}
|
||||
if (entityInfo.Columns.Any(it => it.EntityName == entityInfo.EntityName))
|
||||
{
|
||||
var mappingColumnInfos = this.MappingColumns.Where(it => it.EntityName == entityInfo.EntityName);
|
||||
foreach (var item in entityInfo.Columns.Where(it=>it.IsIgnore==false))
|
||||
{
|
||||
if (!mappingColumnInfos.Any(it => it.PropertyName == item.PropertyName))
|
||||
if (item.PropertyName != item.DbColumnName && item.DbColumnName.IsValuable())
|
||||
this.MappingColumns.Add(item.PropertyName, item.DbColumnName, item.EntityName);
|
||||
}
|
||||
var ignoreInfos = this.IgnoreColumns.Where(it => it.EntityName == entityInfo.EntityName);
|
||||
foreach (var item in entityInfo.Columns.Where(it=>it.IsIgnore))
|
||||
{
|
||||
if (!ignoreInfos.Any(it => it.PropertyName == item.PropertyName))
|
||||
this.IgnoreColumns.Add(item.PropertyName, item.EntityName);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected List<JoinQueryInfo> GetJoinInfos(Expression joinExpression, ref string shortName, params Type[] entityTypeArray)
|
||||
|
||||
@@ -34,7 +34,6 @@ namespace SqlSugar
|
||||
{
|
||||
base.Context = this;
|
||||
base.CurrentConnectionConfig = config;
|
||||
base.InitConstructor();
|
||||
}
|
||||
/// <summary>
|
||||
/// If you do not have system table permissions, use this
|
||||
@@ -44,9 +43,6 @@ namespace SqlSugar
|
||||
{
|
||||
base.Context = this;
|
||||
base.CurrentConnectionConfig = config;
|
||||
Check.ArgumentNullException(config.EntityNamespace, ErrorMessage.EntityNamespaceError);
|
||||
base.EntityNamespace = config.EntityNamespace;
|
||||
base.InitConstructor();
|
||||
}
|
||||
/// <summary>
|
||||
/// Read / write mode. If you have system table permissions, use this
|
||||
@@ -57,7 +53,6 @@ namespace SqlSugar
|
||||
{
|
||||
base.Context = this;
|
||||
base.CurrentConnectionConfig = masterConnectionConfig;
|
||||
base.InitConstructor();
|
||||
if (slaveConnectionConfigs.IsNullOrEmpty()) return;
|
||||
|
||||
var db = this.Ado;
|
||||
@@ -73,12 +68,9 @@ namespace SqlSugar
|
||||
{
|
||||
base.Context = this;
|
||||
base.CurrentConnectionConfig = masterConnectionConfig;
|
||||
base.InitConstructor();
|
||||
if (slaveConnectionConfigs.IsNullOrEmpty()) return;
|
||||
|
||||
var db = this.Ado;
|
||||
Check.ArgumentNullException(masterConnectionConfig.EntityNamespace, ErrorMessage.EntityNamespaceError);
|
||||
base.EntityNamespace = masterConnectionConfig.EntityNamespace;
|
||||
db.MasterConnectionConfig = masterConnectionConfig;
|
||||
db.SlaveConnectionConfigs = slaveConnectionConfigs.ToList();
|
||||
}
|
||||
@@ -133,6 +125,7 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
public virtual ISugarQueryable<T> Queryable<T>() where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T>();
|
||||
var result = InstanceFactory.GetQueryable<T>(base.CurrentConnectionConfig);
|
||||
base.CreateQueryable(result);
|
||||
return result;
|
||||
@@ -159,6 +152,7 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual ISugarQueryable<T,T2> Queryable<T, T2>(Expression<Func<T, T2, object[]>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T,T2>();
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2>(base.CurrentConnectionConfig);
|
||||
base.CreateQueryable(queryable);
|
||||
string shortName = string.Empty;
|
||||
@@ -168,6 +162,7 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual ISugarQueryable<T,T2,T3> Queryable<T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T, T2,T3>();
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2,T3>(base.CurrentConnectionConfig);
|
||||
base.CreateQueryable(queryable);
|
||||
string shortName = string.Empty;
|
||||
@@ -177,6 +172,7 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual ISugarQueryable<T,T2,T3,T4> Queryable<T, T2, T3, T4>(Expression<Func<T, T2, T3, T4, object[]>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T, T2,T3,T4>();
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2,T3,T4>(base.CurrentConnectionConfig);
|
||||
base.CreateQueryable(queryable);
|
||||
string shortName = string.Empty;
|
||||
@@ -238,6 +234,7 @@ namespace SqlSugar
|
||||
#region Insertable
|
||||
public virtual IInsertable<T> Insertable<T>(T[] insertObjs) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T>();
|
||||
var reval = new InsertableProvider<T>();
|
||||
var sqlBuilder = InstanceFactory.GetSqlbuilder(base.CurrentConnectionConfig); ;
|
||||
reval.Context = this;
|
||||
@@ -265,6 +262,7 @@ namespace SqlSugar
|
||||
#region Deleteable
|
||||
public virtual IDeleteable<T> Deleteable<T>() where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T>();
|
||||
var reval = new DeleteableProvider<T>();
|
||||
var sqlBuilder = InstanceFactory.GetSqlbuilder(base.CurrentConnectionConfig); ;
|
||||
reval.Context = this;
|
||||
@@ -280,6 +278,7 @@ namespace SqlSugar
|
||||
#region Updateable
|
||||
public virtual IUpdateable<T> Updateable<T>(T[] UpdateObjs) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T>();
|
||||
var reval = new UpdateableProvider<T>();
|
||||
var sqlBuilder = InstanceFactory.GetSqlbuilder(base.CurrentConnectionConfig); ;
|
||||
reval.Context = this;
|
||||
|
||||
Reference in New Issue
Block a user