Browse Source

Add:

1. Queryable.AsWithAttr()
2. CodeFirst.InitTableWithAttr()
pull/40/head
sunkaixuan 1 year ago
parent
commit
34b9a94ba8
  1. 17
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
  2. 6
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  3. 1
      Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs
  4. 1
      Src/Asp.Net/SqlSugar/Interface/IQueryable.cs
  5. 8
      Src/Asp.Net/SqlSugar/SqlSugarClient.cs

17
Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs

@ -45,7 +45,22 @@ namespace SqlSugar
DefultLength = length;
return this;
}
public void InitTablesWithAttr(params Type[] entityTypes)
{
foreach (var item in entityTypes)
{
var attr = item.GetCustomAttribute<TenantAttribute>();
if (attr==null||this.Context?.Root == null)
{
this.Context.CodeFirst.InitTables(item);
}
else
{
var newDb = this.Context.Root.GetConnectionWithAttr(item);
newDb.CodeFirst.InitTables(item);
}
}
}
public virtual void InitTables(Type entityType)
{
var splitTableAttribute = entityType.GetCustomAttribute<SplitTableAttribute>();

6
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -327,6 +327,12 @@ namespace SqlSugar
var entityName = typeof(T).Name;
return _As(tableName, entityName);
}
public ISugarQueryable<T> AsWithAttr()
{
var asName=GetTableName(this.EntityInfo, this.EntityInfo.DbTableName);
this.QueryBuilder.IsCrossQueryWithAttr = true;
return this.AS(asName);
}
public ISugarQueryable<T> AsType(Type tableNameType)
{
return AS(this.Context.EntityMaintenance.GetEntityInfo(tableNameType).DbTableName);

1
Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs

@ -16,6 +16,7 @@ namespace SqlSugar
void InitTables(string entitiesNamespace);
void InitTables(string[] entitiesNamespaces);
void InitTables(params Type[] entityTypes);
void InitTablesWithAttr(params Type[] entityTypes);
void InitTables(Type entityType);
void InitTables<T>();
void InitTables<T, T2>();

1
Src/Asp.Net/SqlSugar/Interface/IQueryable.cs

@ -19,6 +19,7 @@ namespace SqlSugar
ISugarQueryable<T> Clone();
ISugarQueryable<T> AS<T2>(string tableName);
ISugarQueryable<T> AS(string tableName);
ISugarQueryable<T> AsWithAttr();
ISugarQueryable<T> AsType(Type tableNameType);
ISugarQueryable<T> With(string withString);
//ISugarQueryable<T> CrossQueryWithAttr();

8
Src/Asp.Net/SqlSugar/SqlSugarClient.cs

@ -875,6 +875,14 @@ namespace SqlSugar
var configId = attr.configId;
return this.GetConnection(configId);
}
public SqlSugarProvider GetConnectionWithAttr(Type type)
{
var attr = type.GetCustomAttribute<TenantAttribute>();
if (attr == null)
return this.GetConnection(this.CurrentConnectionConfig.ConfigId);
var configId = attr.configId;
return this.GetConnection(configId);
}
public SqlSugarScopeProvider GetConnectionScopeWithAttr<T>()
{
var attr = typeof(T).GetCustomAttribute<TenantAttribute>();

Loading…
Cancel
Save