嵌套的 UnitOfWork 在 Asp.net 应用程序中不起作用
Nested UnitOfWork is not working in Asp.net application
我有一个场景,我必须在 UnitOfWork 中为一个 table 使用 UnitOfWork 为另一个 table。它以某种方式不起作用。
CurrentUnitOfWork.Save()
按预期工作,但我在 tbl_data table 中更新标志值的代码实际上没有更新标志。
以下是我目前使用的代码结构:
using (UnitOfWork CurrentUnitofWork = new UnitOfWork())
{
int empID=2134;
//Calculation for having a value of data field
....
....
...
CurrentUnitofWork.Save();
if(data==1)
UpdateFlag(1, empID);
else
UpdateFlag(0, empID);
}
这是 UpdateFlag 方法的代码:
public UpdateFlag(int flagValue, int empID)
{
using (UnitOfWork unitOfWork= new UnitOfWork())
{
tbl_data empDetails = unitOfWork.tbl_data.get(x=>x.EmpID==empID).ToList().Single();
if (empDetails != null)
{
empDetails.ActiveFlag= flagValue;
unitOfWork.tbl_data.Update(empDetails);
unitOfWork.Save();
}
}
}
我已经自己解决了这个问题。
using (UnitOfWork CurrentUnitofWork = new UnitOfWork())
{
int empID=2134;
//Calculation for having a value of data field
....
....
...
CurrentUnitofWork.Save();
if(data==1)
UpdateFlag(1, empID);
else
UpdateFlag(0, empID);
CurrentUnitofWork.Save();
}
只需在更新内部工作单元方法后添加一个 CurrentUnitofWork.Save();
,就解决了我的问题。我不确定它背后的逻辑是什么,但它对我有用。
我有一个场景,我必须在 UnitOfWork 中为一个 table 使用 UnitOfWork 为另一个 table。它以某种方式不起作用。
CurrentUnitOfWork.Save()
按预期工作,但我在 tbl_data table 中更新标志值的代码实际上没有更新标志。
以下是我目前使用的代码结构:
using (UnitOfWork CurrentUnitofWork = new UnitOfWork())
{
int empID=2134;
//Calculation for having a value of data field
....
....
...
CurrentUnitofWork.Save();
if(data==1)
UpdateFlag(1, empID);
else
UpdateFlag(0, empID);
}
这是 UpdateFlag 方法的代码:
public UpdateFlag(int flagValue, int empID)
{
using (UnitOfWork unitOfWork= new UnitOfWork())
{
tbl_data empDetails = unitOfWork.tbl_data.get(x=>x.EmpID==empID).ToList().Single();
if (empDetails != null)
{
empDetails.ActiveFlag= flagValue;
unitOfWork.tbl_data.Update(empDetails);
unitOfWork.Save();
}
}
}
我已经自己解决了这个问题。
using (UnitOfWork CurrentUnitofWork = new UnitOfWork())
{
int empID=2134;
//Calculation for having a value of data field
....
....
...
CurrentUnitofWork.Save();
if(data==1)
UpdateFlag(1, empID);
else
UpdateFlag(0, empID);
CurrentUnitofWork.Save();
}
只需在更新内部工作单元方法后添加一个 CurrentUnitofWork.Save();
,就解决了我的问题。我不确定它背后的逻辑是什么,但它对我有用。