Update .net core project

This commit is contained in:
sunkaixuan
2022-08-20 15:18:09 +08:00
parent 2dc1c3a734
commit 064afa409a
3 changed files with 74 additions and 17 deletions

View File

@@ -10,10 +10,12 @@ namespace SqlSugar
public partial class CodeFirstProvider : ICodeFirst public partial class CodeFirstProvider : ICodeFirst
{ {
#region Properties #region Properties
internal static object LockObject = new object();
public virtual SqlSugarProvider Context { get; set; } public virtual SqlSugarProvider Context { get; set; }
protected bool IsBackupTable { get; set; } protected bool IsBackupTable { get; set; }
protected int MaxBackupDataRows { get; set; } protected int MaxBackupDataRows { get; set; }
protected virtual int DefultLength { get; set; } protected virtual int DefultLength { get; set; }
protected Dictionary<Type, string> MappingTables = new Dictionary<Type, string>();
public CodeFirstProvider() public CodeFirstProvider()
{ {
if (DefultLength == 0) if (DefultLength == 0)
@@ -46,29 +48,36 @@ namespace SqlSugar
public virtual void InitTables(Type entityType) public virtual void InitTables(Type entityType)
{ {
//Prevent concurrent requests if used in your program
//this.Context.Utilities.RemoveCacheAll(); lock (CodeFirstProvider.LockObject)
this.Context.InitMappingInfoNoCache(entityType);
if (!this.Context.DbMaintenance.IsAnySystemTablePermissions())
{ {
Check.Exception(true, "Dbfirst and Codefirst requires system table permissions"); MappingTableList oldTableList = CopyMappingTalbe();
} //this.Context.Utilities.RemoveCacheAll();
Check.Exception(this.Context.IsSystemTablesConfig, "Please set SqlSugarClent Parameter ConnectionConfig.InitKeyType=InitKeyType.Attribute "); this.Context.InitMappingInfoNoCache(entityType);
if (!this.Context.DbMaintenance.IsAnySystemTablePermissions())
{
Check.Exception(true, "Dbfirst and Codefirst requires system table permissions");
}
Check.Exception(this.Context.IsSystemTablesConfig, "Please set SqlSugarClent Parameter ConnectionConfig.InitKeyType=InitKeyType.Attribute ");
if (this.Context.Ado.Transaction == null) if (this.Context.Ado.Transaction == null)
{ {
var executeResult = Context.Ado.UseTran(() => var executeResult = Context.Ado.UseTran(() =>
{
Execute(entityType);
});
Check.Exception(!executeResult.IsSuccess, executeResult.ErrorMessage);
}
else
{ {
Execute(entityType); Execute(entityType);
}); }
Check.Exception(!executeResult.IsSuccess, executeResult.ErrorMessage);
} RestMappingTables(oldTableList);
else
{
Execute(entityType);
} }
} }
public void InitTables<T>() public void InitTables<T>()
{ {
InitTables(typeof(T)); InitTables(typeof(T));
@@ -107,6 +116,23 @@ namespace SqlSugar
} }
} }
} }
public ICodeFirst As(Type type, string newTableName)
{
if (!MappingTables.ContainsKey(type))
{
MappingTables.Add(type,newTableName);
}
else
{
MappingTables[type]= newTableName;
}
return this;
}
public ICodeFirst As<T>(string newTableName)
{
return As(typeof(T),newTableName);
}
public virtual void InitTables(string entitiesNamespace) public virtual void InitTables(string entitiesNamespace)
{ {
var types = Assembly.Load(entitiesNamespace).GetTypes(); var types = Assembly.Load(entitiesNamespace).GetTypes();
@@ -180,6 +206,11 @@ namespace SqlSugar
protected virtual void Execute(Type entityType) protected virtual void Execute(Type entityType)
{ {
var entityInfo = this.Context.EntityMaintenance.GetEntityInfoNoCache(entityType); var entityInfo = this.Context.EntityMaintenance.GetEntityInfoNoCache(entityType);
if (this.MappingTables.ContainsKey(entityType))
{
entityInfo.DbTableName = this.MappingTables[entityType];
this.Context.MappingTables.Add(entityInfo.EntityName, entityInfo.DbTableName);
}
if (this.DefultLength > 0) if (this.DefultLength > 0)
{ {
foreach (var item in entityInfo.Columns) foreach (var item in entityInfo.Columns)
@@ -424,6 +455,28 @@ namespace SqlSugar
#endregion #endregion
#region Helper methods #region Helper methods
private void RestMappingTables(MappingTableList oldTableList)
{
this.Context.MappingTables.Clear();
foreach (var table in oldTableList)
{
this.Context.MappingTables.Add(table.EntityName, table.DbTableName);
}
}
private MappingTableList CopyMappingTalbe()
{
MappingTableList oldTableList = new MappingTableList();
if (this.Context.MappingTables == null)
{
this.Context.MappingTables = new MappingTableList();
}
foreach (var table in this.Context.MappingTables)
{
oldTableList.Add(table.EntityName, table.DbTableName);
}
return oldTableList;
}
public virtual string GetCreateTableString(EntityInfo entityInfo) public virtual string GetCreateTableString(EntityInfo entityInfo)
{ {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();

View File

@@ -9,6 +9,10 @@ namespace SqlSugar
SqlSugarProvider Context { get; set; } SqlSugarProvider Context { get; set; }
ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue); ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue);
ICodeFirst SetStringDefaultLength(int length); ICodeFirst SetStringDefaultLength(int length);
ICodeFirst As(Type type,string newTableName);
ICodeFirst As<T>(string newTableName);
void InitTables(string entitiesNamespace); void InitTables(string entitiesNamespace);
void InitTables(string[] entitiesNamespaces); void InitTables(string[] entitiesNamespaces);
void InitTables(params Type[] entityTypes); void InitTables(params Type[] entityTypes);

View File

@@ -47,7 +47,7 @@ namespace SqlSugar
ColumnDescription = item.ColumnDescription, ColumnDescription = item.ColumnDescription,
Length = item.Length, Length = item.Length,
DecimalDigits=item.DecimalDigits, DecimalDigits=item.DecimalDigits,
CreateTableFieldSort=item.CreateTableFieldSort CreateTableFieldSort = item.CreateTableFieldSort
}; };
GetDbType(item, propertyType, result); GetDbType(item, propertyType, result);
if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0) if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)