mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
-
This commit is contained in:
parent
53e9453168
commit
56996fc04d
@ -11,5 +11,6 @@ namespace OrmTest
|
||||
public static string ConnectionString = "server=.;uid=sa;pwd=haosql;database=SQLSUGAR4XTEST";
|
||||
public static string ConnectionString2 = "server=.;uid=sa;pwd=haosql;database=sqlsugar4xtest";
|
||||
public static string ConnectionString3 = "server=.;uid=sa;pwd=haosql;database=sqlsugar4xtesT";
|
||||
public static string ConnectionString4 = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=haosql";
|
||||
}
|
||||
}
|
||||
|
72
Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs
Normal file
72
Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using SqlSugar;
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo1_SqlSugarClient
|
||||
{
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
DistributedTransactionExample();
|
||||
}
|
||||
|
||||
private static void DistributedTransactionExample()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Distributed TransactionExample Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
|
||||
{
|
||||
new ConnectionConfig(){ ConfigId=1, DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },
|
||||
new ConnectionConfig(){ ConfigId=2, DbType=DbType.MySql, ConnectionString=Config.ConnectionString4 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}
|
||||
});
|
||||
|
||||
//use first(SqlServer)
|
||||
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));//
|
||||
db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable<Order>().Count());
|
||||
|
||||
//use mysql
|
||||
db.ChangeDatabase(it => it.DbType == DbType.MySql);
|
||||
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));
|
||||
db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable<Order>().Count());
|
||||
|
||||
//SqlServer
|
||||
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
|
||||
try
|
||||
{
|
||||
db.BeginAllTran();
|
||||
|
||||
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
|
||||
db.Deleteable<Order>().ExecuteCommand();
|
||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql
|
||||
db.Deleteable<Order>().ExecuteCommand();
|
||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
throw new Exception();
|
||||
db.CommitAllTran();
|
||||
}
|
||||
catch
|
||||
{
|
||||
db.RollbackAllTran();
|
||||
Console.WriteLine("---Roll back");
|
||||
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
db.ChangeDatabase(it => it.DbType == DbType.MySql);//use mysql
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
}
|
||||
|
||||
Console.WriteLine("#### Distributed TransactionExample End ####");
|
||||
}
|
||||
}
|
||||
}
|
19
Src/Asp.Net/SqlServerTest/Models/Order.cs
Normal file
19
Src/Asp.Net/SqlServerTest/Models/Order.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
|
||||
public class Order
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
[SqlSugar.SugarColumn(IsNullable =true)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
18
Src/Asp.Net/SqlServerTest/Models/OrderItem.cs
Normal file
18
Src/Asp.Net/SqlServerTest/Models/OrderItem.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
[SqlSugar.SugarTable("OrderDetail")]
|
||||
public class OrderItem
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey =true, IsIdentity =true)]
|
||||
public int ItemId { get; set; }
|
||||
public int OrderId { get; set; }
|
||||
public decimal? Price { get; set; }
|
||||
[SqlSugar.SugarColumn(IsNullable = true)]
|
||||
public DateTime? CreateTime { get; set; }
|
||||
}
|
||||
}
|
@ -10,6 +10,8 @@ namespace OrmTest
|
||||
{
|
||||
//OldTestMain.Init();
|
||||
|
||||
Demo1_SqlSugarClient.Init();
|
||||
|
||||
Console.WriteLine("all successfully.");
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
@ -49,6 +49,9 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Models\Order.cs" />
|
||||
<Compile Include="Models\OrderItem.cs" />
|
||||
<Compile Include="Demo\Demo1_SqlSugarClient.cs" />
|
||||
<Compile Include="OldTest\BugTest\Bug2.cs" />
|
||||
<Compile Include="OldTest\BugTest\BugModels\AccountsModel.cs" />
|
||||
<Compile Include="OldTest\BugTest\BugModels\ClientsModel.cs" />
|
||||
@ -100,9 +103,9 @@
|
||||
<Compile Include="OldTest\PerformanceTesting\SqlSugarPerformance.cs" />
|
||||
<Compile Include="OldTest\T4\SugarTemplate1.cs" />
|
||||
<Compile Include="OldTest\T4\SugarTemplate2.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>SugarTemplate.tt</DependentUpon>
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
</Compile>
|
||||
<Compile Include="OldTest\UnitTest\DataTest.cs" />
|
||||
<Compile Include="OldTest\UnitTest\Delete.cs" />
|
||||
@ -145,6 +148,7 @@
|
||||
<ItemGroup>
|
||||
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
@ -57,12 +57,12 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual int CommandTimeOut { get; set; }
|
||||
public virtual CommandType CommandType { get; set; }
|
||||
public virtual bool IsEnableLogEvent {get;set;}
|
||||
public virtual bool IsEnableLogEvent { get; set; }
|
||||
public virtual bool IsClearParameters { get; set; }
|
||||
public virtual Action<string, SugarParameter[]> LogEventStarting=> this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuting;
|
||||
public virtual Action<string, SugarParameter[]> LogEventStarting => this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuting;
|
||||
public virtual Action<string, SugarParameter[]> LogEventCompleted => this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted;
|
||||
public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL => this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql;
|
||||
protected virtual Func<string,string> FormatSql { get; set; }
|
||||
protected virtual Func<string, string> FormatSql { get; set; }
|
||||
public virtual Action<SqlSugarException> ErrorEvent => this.Context.CurrentConnectionConfig.AopEvents.OnError;
|
||||
public virtual Action<DiffLogModel> DiffLogEvent => this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent;
|
||||
public virtual List<IDbConnection> SlaveConnections { get; set; }
|
||||
@ -147,12 +147,14 @@ namespace SqlSugar
|
||||
public virtual void BeginTran()
|
||||
{
|
||||
CheckConnection();
|
||||
this.Transaction = this.Connection.BeginTransaction();
|
||||
if (this.Transaction == null)
|
||||
this.Transaction = this.Connection.BeginTransaction();
|
||||
}
|
||||
public virtual void BeginTran(IsolationLevel iso)
|
||||
{
|
||||
CheckConnection();
|
||||
this.Transaction = this.Connection.BeginTransaction(iso);
|
||||
if (this.Transaction == null)
|
||||
this.Transaction = this.Connection.BeginTransaction(iso);
|
||||
}
|
||||
public virtual void RollbackTran()
|
||||
{
|
||||
@ -185,7 +187,7 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Use
|
||||
public DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack=null)
|
||||
public DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null)
|
||||
{
|
||||
var result = new DbResult<bool>();
|
||||
try
|
||||
@ -214,7 +216,7 @@ namespace SqlSugar
|
||||
{
|
||||
Task<DbResult<bool>> result = new Task<DbResult<bool>>(() =>
|
||||
{
|
||||
return UseTran(action,errorCallBack);
|
||||
return UseTran(action, errorCallBack);
|
||||
});
|
||||
TaskStart(result);
|
||||
return result;
|
||||
@ -249,7 +251,7 @@ namespace SqlSugar
|
||||
{
|
||||
Task<DbResult<T>> result = new Task<DbResult<T>>(() =>
|
||||
{
|
||||
return UseTran(action,errorCallBack);
|
||||
return UseTran(action, errorCallBack);
|
||||
});
|
||||
TaskStart(result);
|
||||
return result;
|
||||
@ -297,7 +299,7 @@ namespace SqlSugar
|
||||
try
|
||||
{
|
||||
InitParameters(ref sql, parameters);
|
||||
if (FormatSql != null)
|
||||
if (FormatSql != null)
|
||||
sql = FormatSql(sql);
|
||||
SetConnectionStart(sql);
|
||||
if (this.ProcessingEventStartingSQL != null)
|
||||
@ -421,7 +423,7 @@ namespace SqlSugar
|
||||
{
|
||||
try
|
||||
{
|
||||
InitParameters(ref sql,parameters);
|
||||
InitParameters(ref sql, parameters);
|
||||
if (FormatSql != null)
|
||||
sql = FormatSql(sql);
|
||||
SetConnectionStart(sql);
|
||||
@ -440,7 +442,7 @@ namespace SqlSugar
|
||||
{
|
||||
CommandType = CommandType.Text;
|
||||
if (ErrorEvent != null)
|
||||
ExecuteErrorEvent(sql,parameters,ex);
|
||||
ExecuteErrorEvent(sql, parameters, ex);
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
@ -943,19 +945,22 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual void ExecuteBefore(string sql, SugarParameter[] parameters)
|
||||
{
|
||||
if (this.Context.IsAsyncMethod==false&&this.Context.CurrentConnectionConfig.Debugger != null && this.Context.CurrentConnectionConfig.Debugger.EnableThreadSecurityValidation == true) {
|
||||
if (this.Context.IsAsyncMethod == false && this.Context.CurrentConnectionConfig.Debugger != null && this.Context.CurrentConnectionConfig.Debugger.EnableThreadSecurityValidation == true)
|
||||
{
|
||||
|
||||
var contextId =this.Context.ContextID.ToString();
|
||||
var contextId = this.Context.ContextID.ToString();
|
||||
var processId = Thread.CurrentThread.ManagedThreadId.ToString();
|
||||
var cache = new ReflectionInoCacheService();
|
||||
if (!cache.ContainsKey<string>(contextId))
|
||||
{
|
||||
cache.Add(contextId, processId);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
var cacheValue = cache.Get<string>(contextId);
|
||||
if (processId != cacheValue) {
|
||||
throw new SqlSugarException(this.Context,ErrorMessage.GetThrowMessage("Detection of SqlSugarClient cross-threading usage,a thread needs a new one", "检测到声名的SqlSugarClient跨线程使用,请检查是否静态、是否单例、或者IOC配置错误引起的,保证一个线程new出一个对象 ,具本Sql:")+sql,parameters);
|
||||
if (processId != cacheValue)
|
||||
{
|
||||
throw new SqlSugarException(this.Context, ErrorMessage.GetThrowMessage("Detection of SqlSugarClient cross-threading usage,a thread needs a new one", "检测到声名的SqlSugarClient跨线程使用,请检查是否静态、是否单例、或者IOC配置错误引起的,保证一个线程new出一个对象 ,具本Sql:") + sql, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -982,11 +987,12 @@ namespace SqlSugar
|
||||
var hasParameter = parameters.HasValue();
|
||||
if (hasParameter)
|
||||
{
|
||||
foreach (var outputParameter in parameters.Where(it => it.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput,ParameterDirection.ReturnValue)))
|
||||
foreach (var outputParameter in parameters.Where(it => it.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue)))
|
||||
{
|
||||
var gobalOutputParamter = this.OutputParameters.FirstOrDefault(it => it.ParameterName == outputParameter.ParameterName);
|
||||
if (gobalOutputParamter == null) {//Oracle bug
|
||||
gobalOutputParamter=this.OutputParameters.FirstOrDefault(it => it.ParameterName == outputParameter.ParameterName.TrimStart(outputParameter.ParameterName.First()));
|
||||
if (gobalOutputParamter == null)
|
||||
{//Oracle bug
|
||||
gobalOutputParamter = this.OutputParameters.FirstOrDefault(it => it.ParameterName == outputParameter.ParameterName.TrimStart(outputParameter.ParameterName.First()));
|
||||
}
|
||||
outputParameter.Value = gobalOutputParamter.Value;
|
||||
this.OutputParameters.Remove(gobalOutputParamter);
|
||||
@ -1028,12 +1034,12 @@ namespace SqlSugar
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.HasValue()&& this.IsDisableMasterSlaveSeparation==false;
|
||||
return this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.HasValue() && this.IsDisableMasterSlaveSeparation == false;
|
||||
}
|
||||
}
|
||||
private void SetConnectionStart(string sql)
|
||||
{
|
||||
if (this.Transaction==null&&this.IsMasterSlaveSeparation && IsRead(sql))
|
||||
if (this.Transaction == null && this.IsMasterSlaveSeparation && IsRead(sql))
|
||||
{
|
||||
if (this.MasterConnection == null)
|
||||
{
|
||||
@ -1063,7 +1069,7 @@ namespace SqlSugar
|
||||
|
||||
private void SetConnectionEnd(string sql)
|
||||
{
|
||||
if (this.IsMasterSlaveSeparation && IsRead(sql)&&this.Transaction==null)
|
||||
if (this.IsMasterSlaveSeparation && IsRead(sql) && this.Transaction == null)
|
||||
{
|
||||
this.Connection = this.MasterConnection;
|
||||
this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnection.ConnectionString;
|
||||
@ -1079,9 +1085,9 @@ namespace SqlSugar
|
||||
|
||||
private void ExecuteErrorEvent(string sql, SugarParameter[] parameters, Exception ex)
|
||||
{
|
||||
ErrorEvent(new SqlSugarException(this.Context,ex, sql, parameters));
|
||||
ErrorEvent(new SqlSugarException(this.Context, ex, sql, parameters));
|
||||
}
|
||||
private void InitParameters(ref string sql, SugarParameter[] parameters)
|
||||
private void InitParameters(ref string sql, SugarParameter[] parameters)
|
||||
{
|
||||
if (parameters.HasValue())
|
||||
{
|
||||
@ -1103,7 +1109,7 @@ namespace SqlSugar
|
||||
}
|
||||
if (item.ParameterName.Substring(0, 1) == ":")
|
||||
{
|
||||
sql = sql.Replace("@"+item.ParameterName.Substring(1), newValues.ToArray().ToJoinSqlInVals());
|
||||
sql = sql.Replace("@" + item.ParameterName.Substring(1), newValues.ToArray().ToJoinSqlInVals());
|
||||
}
|
||||
sql = sql.Replace(item.ParameterName, newValues.ToArray().ToJoinSqlInVals());
|
||||
item.Value = DBNull.Value;
|
||||
|
@ -12,6 +12,13 @@ namespace SqlSugar
|
||||
protected bool IsBackupTable { get; set; }
|
||||
protected int MaxBackupDataRows { get; set; }
|
||||
protected virtual int DefultLength { get; set; }
|
||||
public CodeFirstProvider()
|
||||
{
|
||||
if (DefultLength == 0)
|
||||
{
|
||||
DefultLength = 255;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Public methods
|
||||
@ -50,15 +57,15 @@ namespace SqlSugar
|
||||
}
|
||||
public void InitTables<T, T2>()
|
||||
{
|
||||
InitTables(typeof(T),typeof(T2));
|
||||
InitTables(typeof(T), typeof(T2));
|
||||
}
|
||||
public void InitTables<T, T2, T3>()
|
||||
{
|
||||
InitTables(typeof(T), typeof(T2),typeof(T3));
|
||||
InitTables(typeof(T), typeof(T2), typeof(T3));
|
||||
}
|
||||
public void InitTables<T, T2, T3, T4>()
|
||||
{
|
||||
InitTables(typeof(T), typeof(T2), typeof(T3),typeof(T4));
|
||||
InitTables(typeof(T), typeof(T2), typeof(T3), typeof(T4));
|
||||
}
|
||||
public virtual void InitTables(params Type[] entityTypes)
|
||||
{
|
||||
|
@ -237,7 +237,7 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual bool AddTableRemark(string tableName, string description)
|
||||
{
|
||||
string sql = string.Format(this.AddTableRemarkSql, tableName, description);
|
||||
string sql = string.Format(this.AddTableRemarkSql,this.SqlBuilder.GetTranslationTableName(tableName), description);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ namespace SqlSugar
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string ConfigId { get; set; }
|
||||
public dynamic ConfigId { get; set; }
|
||||
/// <summary>
|
||||
///DbType.SqlServer Or Other
|
||||
/// </summary>
|
||||
|
@ -53,7 +53,7 @@ namespace SqlSugar
|
||||
{
|
||||
var allConfigs = _AllClients.Select(it => it.ConnectionConfig);
|
||||
Check.Exception(!allConfigs.Any(changeExpression), "changeExpression was not found {0}", changeExpression.ToString());
|
||||
InitContext(allConfigs.First(changeExpression));
|
||||
InitTerant(_AllClients.First(it=>it.ConnectionConfig==allConfigs.First(changeExpression)));
|
||||
if (this._IsAllTran)
|
||||
this.Ado.BeginTran();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user