测试 sqlserver mysql 的批量处理

This commit is contained in:
zhougf committed 2024-03-04 16:16:25 +08:00
1 parent 7fa788d47e
commit 778f3e04f6
7 files changed
+75 -39

No files matched your search

+49 -23
View File
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using HHWMS.Web.Models;
using Microsoft.EntityFrameworkCore;
using NPOI.POIFS.FileSystem;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Z.EntityFramework.Plus;
@@ -20,6 +22,8 @@ namespace HHWMS.Web
_info = context.LoginUserInfo;
}
#region find
public T FindSingle<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().AsNoTracking().FirstOrDefault(where);
@@ -47,9 +51,13 @@ namespace HHWMS.Web
public int GetCount<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().Where(where).Count();
return _data.Set<T>().Count(where);
}
#endregion find
#region insert
public new int Add<T>(T entity) where T : class
{
if (typeof(T).IsSubclassOf(typeof(BasePoco)))
@@ -67,7 +75,7 @@ namespace HHWMS.Web
return _data.SaveChanges();
}
public int BatchAdd<T>(T[] entities) where T : class
public int BatchAdd<T>(IEnumerable<T> entities) where T : class
{
foreach (T entity in entities)
{
@@ -90,16 +98,13 @@ namespace HHWMS.Web
public int AddToHistory<T>(string name, Expression<Func<T, bool>> where, Expression<Func<T, object>> entity) where T : class
{
var query = _data.Set<T>().Where(where);
var count = query.InsertFromQuery(typeof(T).Name + "History", entity);
var count1 = _data.Set<T>().DeleteRangeByKey(query.ToArray());
if(count == count1)
{
return count;
}
return 0;
return _data.Set<T>().Where(where).InsertFromQuery(name,entity);
}
#endregion insert
#region update
public new int Update<T>(T entity) where T : class
{
if (typeof(T).IsSubclassOf(typeof(BasePoco)))
@@ -111,7 +116,7 @@ namespace HHWMS.Web
return _data.SaveChanges();
}
public int BatchUpdate<T>(T[] entities) where T : class
public int BatchUpdate<T>(IEnumerable<T> entities) where T : class
{
foreach (T entity in entities)
{
@@ -134,40 +139,61 @@ namespace HHWMS.Web
public int BatchUpdate<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T : class
{
/*if (typeof(T).IsSubclassOf(typeof(BasePoco)))
{
(entity as BasePoco).UpdateTime = DateTime.Now;
(entity as BasePoco).UpdateBy = _info?.ITCode;
}*/
return _data.Set<T>().Where(where).Update(entity);
}
#endregion update
#region delete
public int Delete<T>(T entity) where T : class
{
_data.Set<T>().Remove(entity);
return _data.SaveChanges();
}
public int BatchDelete<T>(IEnumerable<T> entities) where T : class
{
if (_data.DBType.Equals(DBTypeEnum.SqlServer))
{
return _data.Set<T>().DeleteRangeByKey(entities);
}
else
{
_data.Set<T>().RemoveRange(entities);
return _data.SaveChanges();
}
}
public int BatchDelete<T>(Expression<Func<T, bool>> where) where T : class
{
var query = _data.Set<T>().Where(where);
return _data.Set<T>().DeleteRangeByKey(query.ToArray());
return _data.Set<T>().Where(where).Delete();
}
/// <summary>
/// 测试 不支持mysql
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
public int DeleteById<T>(T entity) where T : class
{
return _data.Set<T>().DeleteByKey(entity);
return _data.Set<T>().DeleteByKey((entity as BasePoco).ID);
}
/// <summary>
/// 测试 不支持mysql
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <returns></returns>
public int DeleteById<T>(int id) where T : class
{
return _data.Set<T>().DeleteByKey(id);
}
public int BatchDelete<T>(T[] entities) where T : class
{
return _data.Set<T>().DeleteRangeByKey(entities);
}
#endregion delete
public string GetSerialNumber(string prefix, int length = 4)
{