mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 01:58:13 +08:00
Update Core
This commit is contained in:
@@ -54,11 +54,20 @@ namespace SqlSugar
|
||||
Check.Exception(true, "Dbfirst and Codefirst requires system table permissions");
|
||||
}
|
||||
Check.Exception(this.Context.IsSystemTablesConfig, "Please set SqlSugarClent Parameter ConnectionConfig.InitKeyType=InitKeyType.Attribute ");
|
||||
var executeResult = Context.Ado.UseTran(() =>
|
||||
|
||||
if (this.Context.Ado.Transaction == null)
|
||||
{
|
||||
var executeResult = Context.Ado.UseTran(() =>
|
||||
{
|
||||
Execute(entityType);
|
||||
});
|
||||
Check.Exception(!executeResult.IsSuccess, executeResult.ErrorMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
Execute(entityType);
|
||||
});
|
||||
Check.Exception(!executeResult.IsSuccess, executeResult.ErrorMessage);
|
||||
}
|
||||
|
||||
}
|
||||
public void InitTables<T>()
|
||||
{
|
||||
|
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class FastestProvider<T>:IFastest<T>
|
||||
{
|
||||
private SqlSugarProvider context;
|
||||
private ISugarQueryable<T> queryable;
|
||||
|
||||
public FastestProvider(SqlSugarProvider sqlSugarProvider)
|
||||
{
|
||||
this.context = sqlSugarProvider;
|
||||
this.queryable = this.context.Queryable<T>();
|
||||
}
|
||||
public int BulkCopy(List<T> datas)
|
||||
{
|
||||
return BulkCopyAsync(datas).GetAwaiter().GetResult();
|
||||
}
|
||||
public async Task<int> BulkCopyAsync(List<T> datas)
|
||||
{
|
||||
|
||||
DataTable tempDataTable = ReflectionInoCore<DataTable>.GetInstance().GetOrCreate("BulkCopyAsync" + typeof(T).FullName,()=> queryable.Where(it=>false).ToDataTable());
|
||||
var dt = new DataTable();
|
||||
foreach (DataColumn item in tempDataTable.Columns)
|
||||
{
|
||||
dt.Columns.Add(item.ColumnName,item.DataType);
|
||||
}
|
||||
var entityInfo = this.context.EntityMaintenance.GetEntityInfo<T>();
|
||||
dt.TableName =queryable.SqlBuilder.GetTranslationTableName(entityInfo.DbTableName);
|
||||
var columns = entityInfo.Columns;
|
||||
foreach (var item in datas)
|
||||
{
|
||||
var dr = dt.NewRow();
|
||||
foreach (var column in columns)
|
||||
{
|
||||
if (column.IsIgnore || column.IsOnlyIgnoreInsert)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var name = column.DbColumnName;
|
||||
if (name == null)
|
||||
{
|
||||
name = column.PropertyName;
|
||||
}
|
||||
var value = ValueConverter(column, PropertyCallAdapterProvider<T>.GetInstance(column.PropertyName).InvokeGet(item));
|
||||
dr[name] = value;
|
||||
}
|
||||
dt.Rows.Add(dr);
|
||||
}
|
||||
IFastBuilder buider = new SqlServerFastBuilder();
|
||||
buider.Context = context;
|
||||
var result= await buider.ExecuteBulkCopyAsync(dt);
|
||||
return result;
|
||||
}
|
||||
|
||||
private object ValueConverter(EntityColumnInfo columnInfo,object value)
|
||||
{
|
||||
if (value == null)
|
||||
return value;
|
||||
if (value is DateTime&&(DateTime)value == DateTime.MinValue)
|
||||
{
|
||||
value = Convert.ToDateTime("1900-01-01");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1175,7 +1175,13 @@ namespace SqlSugar
|
||||
Check.Exception(true,ErrorMessage.GetThrowMessage("Child objects do not support tenant methods, var childDb= Db.GetConnection(confid) ,Db is master ", "Db子对象不支持租户方法,请使用主对象,例如:var childDb= Db.GetConnection(confid) Db是主对象,childDb是子对象 "));
|
||||
return null;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
#region
|
||||
public IFastest<T> Fastest<T>()
|
||||
{
|
||||
return new FastestProvider<T>(this);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IFastBuilder
|
||||
{
|
||||
SqlSugarProvider Context { get; set; }
|
||||
|
||||
Task<int> ExecuteBulkCopyAsync(DataTable dt);
|
||||
}
|
||||
}
|
13
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IFastest.cs
Normal file
13
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IFastest.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IFastest<T>
|
||||
{
|
||||
int BulkCopy(List<T> datas);
|
||||
Task<int> BulkCopyAsync(List<T> datas);
|
||||
}
|
||||
}
|
@@ -176,5 +176,10 @@ namespace SqlSugar
|
||||
#region Cache
|
||||
SugarCacheProvider DataCache { get; }
|
||||
#endregion
|
||||
|
||||
#region Fastest
|
||||
IFastest<T> Fastest<T>();
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerFastBuilder: IFastBuilder
|
||||
{
|
||||
|
||||
public SqlSugarProvider Context { get; set; }
|
||||
|
||||
|
||||
public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
|
||||
{
|
||||
|
||||
SqlBulkCopy bulkCopy = GetBulkCopyInstance();
|
||||
bulkCopy.DestinationTableName = dt.TableName;
|
||||
try
|
||||
{
|
||||
await bulkCopy.WriteToServerAsync(dt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CloseDb();
|
||||
throw ex;
|
||||
}
|
||||
CloseDb();
|
||||
return dt.Rows.Count;
|
||||
}
|
||||
public SqlBulkCopy GetBulkCopyInstance()
|
||||
{
|
||||
SqlBulkCopy copy;
|
||||
if (this.Context.Ado.Transaction == null)
|
||||
{
|
||||
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.CheckConstraints, (SqlTransaction)this.Context.Ado.Transaction);
|
||||
}
|
||||
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
|
||||
{
|
||||
this.Context.Ado.Connection.Open();
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
public void CloseDb()
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -565,6 +565,13 @@ namespace SqlSugar
|
||||
|
||||
#endregion
|
||||
|
||||
#region
|
||||
public IFastest<T> Fastest<T>()
|
||||
{
|
||||
return this.Context.Fastest<T>();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region More api
|
||||
public IContextMethods Utilities { get { return this.Context.Utilities; } set { this.Context.Utilities = value; } }
|
||||
public AopProvider Aop => this.Context.Aop;
|
||||
|
@@ -641,6 +641,10 @@ namespace SqlSugar
|
||||
return ScopedContext.IsAnyConnection(configId);
|
||||
}
|
||||
|
||||
public IFastest<T> Fastest<T>()
|
||||
{
|
||||
return ScopedContext.Fastest<T>();
|
||||
}
|
||||
private SqlSugarClient GetContext()
|
||||
{
|
||||
SqlSugarClient result = null;
|
||||
|
@@ -25,8 +25,8 @@ namespace SqlSugar
|
||||
}
|
||||
public class PropertyCallAdapterProvider<TThis>
|
||||
{
|
||||
private static readonly Dictionary<string, IPropertyCallAdapter<TThis>> _instances =
|
||||
new Dictionary<string, IPropertyCallAdapter<TThis>>();
|
||||
private static readonly System.Collections.Concurrent.ConcurrentDictionary<string, IPropertyCallAdapter<TThis>> _instances =
|
||||
new System.Collections.Concurrent.ConcurrentDictionary<string, IPropertyCallAdapter<TThis>>();
|
||||
|
||||
public static IPropertyCallAdapter<TThis> GetInstance(string forPropertyName)
|
||||
{
|
||||
@@ -60,7 +60,7 @@ namespace SqlSugar
|
||||
.CreateInstance(concreteAdapterType, getterInvocation)
|
||||
as IPropertyCallAdapter<TThis>;
|
||||
|
||||
_instances.Add(forPropertyName, instance);
|
||||
_instances.GetOrAdd(forPropertyName, instance);
|
||||
}
|
||||
|
||||
return instance;
|
||||
|
Reference in New Issue
Block a user