110,535
社区成员
发帖
与我相关
我的任务
分享
await wc.IsWPName(hs);
public async Task IsWPName(HashSet<string> hs)
{
using (var wcSelvice = new DAL.WorkCenterSelvice())
{
//分离出工作中心
List<string> wcName = new List<string>();
//分离出工序
List<string> wpName = new List<string>();
foreach (var item in hs)
{
string[] s = item.Split(',');
wcName.Add(s[0]);
wpName.Add(s[1]);
}
//转换工序ID
List<int> wpID = new List<int>();
using (var wpSelvice = new DAL.WP_TableSelvice())
{
foreach (var item in wpName)
{
wpID.Add(wpSelvice.GetAllWhere(w => w.WP_Name == item).Select(s => s.Id).FirstOrDefault());
}
}
//判断工作中心中是否存在该工序 若不存在则添加
for (int i = 0; i < wcName.Count; i++)
{
string wcNamestr = wcName[i];
//得到工作中心数据
var wcS = wcSelvice.GetWhere(w => w.WCName == wcNamestr).FirstOrDefault();
////查询时已经用到AsNoTracking()
string temps = wcS.WPID;
// wcS.WPID = "1,2,3,4,5,6";
//判断是否为空wpID
if (wcS.WPID == null)
{
wcS.WPID = wpID[i] + ",";
}
else
{
bool bo = true;
//分离出工作中心的工序 判断此次添加的工序是否存在
string[] st = temps.Split(',');
for (int j = 0; j < st.Length; j++)
{
if (st[j] == "") continue;
else
{
if (st[j] == wpID[i].ToString())
{
bo = false;
break;
}
}
}
//添加的工序不存在该工作中心
if (bo)
{
string tempwp = temps + wpID[i] + ",";
wcS.WPID = tempwp;
}
else
{
continue;
}
}
//修改
await wcSelvice.EditAsync(wcS, true);
}
}
}
当数据库WPID字段Wie空时可以修改成功 但是一旦有数据了就失败了
但是第二次运行程序它可以走过第一次错的数据 然后下一段数据继续报错
public async Task EditAsync(T t, bool saved = true) //异步操作
{
DbContext.Configuration.ValidateOnSaveEnabled = false; //关闭校验
DbContext.Entry(t).State = System.Data.Entity.EntityState.Modified;
if (saved)
{
await DbContext.SaveChangesAsync();
DbContext.Configuration.ValidateOnSaveEnabled = true; //关闭校验
}
}
DbContext.Set<T>().Attach(wcS);
await DbContext.SaveChangesAsync();