Skip to content

Commit e23262f

Browse files
committed
修复dapper事务嵌套异常的问题
1 parent 572a79a commit e23262f

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

Vue.Net/VOL.Core/Dapper/SqlDapper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public SqlMapper.GridReader QueryMultiple(string cmd, object param, CommandType?
168168

169169
private T Execute<T>(Func<IDbConnection, IDbTransaction, T> func, bool beginTransaction = false, bool disposeConn = true)
170170
{
171-
if (beginTransaction)
171+
if (beginTransaction && !_transaction)
172172
{
173173
Connection.Open();
174174
dbTransaction = Connection.BeginTransaction();
@@ -207,19 +207,19 @@ private T Execute<T>(Func<IDbConnection, IDbTransaction, T> func, bool beginTran
207207
/// </summary>
208208
/// <typeparam name="T"></typeparam>
209209
/// <param name="entity"></param>
210-
/// <param name="updateFileds">指定插入的字段</param>
210+
/// <param name="addFileds">指定插入的字段</param>
211211
/// <param name="beginTransaction">是否开启事务</param>
212212
/// <returns></returns>
213-
public int Add<T>(T entity, Expression<Func<T, object>> updateFileds = null, bool beginTransaction = false)
213+
public int Add<T>(T entity, Expression<Func<T, object>> addFileds = null, bool beginTransaction = false)
214214
{
215-
return AddRange<T>(new T[] { entity });
215+
return AddRange<T>(new T[] { entity }, addFileds, beginTransaction);
216216
}
217217
/// <summary>
218218
///
219219
/// </summary>
220220
/// <typeparam name="T"></typeparam>
221221
/// <param name="entities"></param>
222-
/// <param name="updateFileds">指定插入的字段</param>
222+
/// <param name="addFileds">指定插入的字段</param>
223223
/// <param name="beginTransaction">是否开启事务</param>
224224
/// <returns></returns>
225225
public int AddRange<T>(IEnumerable<T> entities, Expression<Func<T, object>> addFileds = null, bool beginTransaction = true)
@@ -269,7 +269,7 @@ public int AddRange<T>(IEnumerable<T> entities, Expression<Func<T, object>> addF
269269
return Execute<int>((conn, dbTransaction) =>
270270
{
271271
//todo pgsql待实现
272-
return conn.Execute(sql, (DBType.Name == DbCurrentType.MySql.ToString() || DBType.Name == DbCurrentType.PgSql.ToString()) ? entities.ToList() : null);
272+
return conn.Execute(sql, (DBType.Name == DbCurrentType.MySql.ToString() || DBType.Name == DbCurrentType.PgSql.ToString()) ? entities.ToList() : null,dbTransaction);
273273
}, beginTransaction);
274274
}
275275

@@ -337,7 +337,7 @@ public int DelWithKey<T>(bool beginTransaction = false, params object[] keys)
337337

338338
IEnumerable<(bool, string, object)> validation = keyProperty.ValidationValueForDbType(keys);
339339
if (validation.Any(x => !x.Item1))
340-
{
340+
{
341341
throw new Exception($"主键类型【{validation.Where(x => !x.Item1).Select(s => s.Item3).FirstOrDefault()}】不正确");
342342
}
343343
string tKey = entityType.GetKeyProperty().Name;

开发版dev/Vue.NetCore/Vue.Net/VOL.Core/Dapper/SqlDapper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public SqlMapper.GridReader QueryMultiple(string cmd, object param, CommandType?
168168

169169
private T Execute<T>(Func<IDbConnection, IDbTransaction, T> func, bool beginTransaction = false, bool disposeConn = true)
170170
{
171-
if (beginTransaction)
171+
if (beginTransaction && !_transaction)
172172
{
173173
Connection.Open();
174174
dbTransaction = Connection.BeginTransaction();
@@ -207,19 +207,19 @@ private T Execute<T>(Func<IDbConnection, IDbTransaction, T> func, bool beginTran
207207
/// </summary>
208208
/// <typeparam name="T"></typeparam>
209209
/// <param name="entity"></param>
210-
/// <param name="updateFileds">指定插入的字段</param>
210+
/// <param name="addFileds">指定插入的字段</param>
211211
/// <param name="beginTransaction">是否开启事务</param>
212212
/// <returns></returns>
213-
public int Add<T>(T entity, Expression<Func<T, object>> updateFileds = null, bool beginTransaction = false)
213+
public int Add<T>(T entity, Expression<Func<T, object>> addFileds = null, bool beginTransaction = false)
214214
{
215-
return AddRange<T>(new T[] { entity });
215+
return AddRange<T>(new T[] { entity }, addFileds, beginTransaction);
216216
}
217217
/// <summary>
218218
///
219219
/// </summary>
220220
/// <typeparam name="T"></typeparam>
221221
/// <param name="entities"></param>
222-
/// <param name="updateFileds">指定插入的字段</param>
222+
/// <param name="addFileds">指定插入的字段</param>
223223
/// <param name="beginTransaction">是否开启事务</param>
224224
/// <returns></returns>
225225
public int AddRange<T>(IEnumerable<T> entities, Expression<Func<T, object>> addFileds = null, bool beginTransaction = true)
@@ -269,7 +269,7 @@ public int AddRange<T>(IEnumerable<T> entities, Expression<Func<T, object>> addF
269269
return Execute<int>((conn, dbTransaction) =>
270270
{
271271
//todo pgsql待实现
272-
return conn.Execute(sql, (DBType.Name == DbCurrentType.MySql.ToString() || DBType.Name == DbCurrentType.PgSql.ToString()) ? entities.ToList() : null);
272+
return conn.Execute(sql, (DBType.Name == DbCurrentType.MySql.ToString() || DBType.Name == DbCurrentType.PgSql.ToString()) ? entities.ToList() : null,dbTransaction);
273273
}, beginTransaction);
274274
}
275275

@@ -337,7 +337,7 @@ public int DelWithKey<T>(bool beginTransaction = false, params object[] keys)
337337

338338
IEnumerable<(bool, string, object)> validation = keyProperty.ValidationValueForDbType(keys);
339339
if (validation.Any(x => !x.Item1))
340-
{
340+
{
341341
throw new Exception($"主键类型【{validation.Where(x => !x.Item1).Select(s => s.Item3).FirstOrDefault()}】不正确");
342342
}
343343
string tKey = entityType.GetKeyProperty().Name;

0 commit comments

Comments
 (0)