LINQ 删除多表数据、检查数据引用
LINQ 删除多表数据、检查数据引用
LINQ 删除多表数据、检查数据引用
C# 全选
/// <summary>
/// 删除客户资料
/// </summary>
/// <param name="keyValue">主键isid</param>
/// <returns></returns>
public override bool Delete(string keyValue)
{
var obj = _Database.GetQueryable<dt_Customer>().Where(w => w.isid == keyValue).FirstOrDefault();
if (obj != null)
{
//检查数据引用
if (this.IsReference(obj.CustomerCode))
{
throw new BizException("当前记录不可删除,资料已使用!");
}
}
//启用事务删除多表数据
try
{
_Database.BeginTransaction();
var master = _Database.GetQueryable<dt_Customer>().Where(w => w.isid == keyValue).FirstOrDefault();
var detailA = _Database.GetQueryable<dt_Address>().Where(w => w.OwnerCode == master.CustomerCode).ToList();
var detailB = _Database.GetQueryable<dt_CustomerPriceList>().Where(w => w.CustomerCode == master.CustomerCode).ToList();
var detailC = _Database.GetQueryable<dt_CustomerDiscount>().Where(w => w.CustomerCode == master.CustomerCode).ToList();
var detailD = _Database.GetQueryable<tb_AttachFile>().Where(w => w.DocType == "Customer" && w.DocID == master.CustomerCode).ToList();
var detailE = _Database.GetQueryable<dt_CustomerCartons>().Where(w => w.CustomerCode == master.CustomerCode).ToList();
int count = 0;
count += _Database.Remove<dt_Customer>(master);
count += _Database.Remove<dt_Address>(detailA);
count += _Database.Remove<dt_CustomerPriceList>(detailB);
count += _Database.Remove<dt_CustomerDiscount>(detailC);
count += _Database.Remove<tb_AttachFile>(detailD);
count += _Database.Remove<dt_CustomerCartons>(detailE);
_Database.CommitTransaction();
//保存被删除的数据
if (count > 0)
_Log.Log(DataLogType.delete, "Customer删除成功:", nameof(dt_Customer), master.CustomerCode, _Loginer.Account, master, master, detailA, detailB, detailC, detailD, detailE);
return count > 0;
}
catch (Exception e)
{
_Database.RollbackTransaction();
throw;
}
}
检查数据引用
C# 全选
/// <summary>
/// 检查数据引用
/// </summary>
/// <param name="customerCode"></param>
/// <returns></returns>
private bool IsReference(string customerCode)
{
if (_Database.GetQueryable<tb_SO>().Any(a => a.CustomerCode == customerCode)) return true;//销售单
if (_Database.GetQueryable<tb_DO>().Any(a => a.CustomerCode == customerCode)) return true;//送货单
if (_Database.GetQueryable<tb_QO>().Any(a => a.CustomerCode == customerCode)) return true;//报价单
return false;
}
删除客户资料截图
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
CSFramework.EF C/S框架网