读书人

linq 批改提交给事物拦截后再次提交不

发布时间: 2012-12-17 09:31:40 作者: rapoo

linq 修改提交给事物拦截后再次提交不更新数据库


bool bo = false;
private void simpleButton1_Click(object sender, EventArgs e)
{
using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
{
try
{
CoreProject.Service.EmployeeService.GetSObj.aa(userState.Employee, bo);


scope.Complete();//提交事务
}
catch (Exception ee)
{
bo = true;
this.ShowMessageBox(ee.Message);
}
finally
{
scope.Dispose();//回滚事务
}
}
}

//-----------在EmployeeService类中的方法

public void UpdateName(Employee employee, bool bo)
{
var obj = (from d in db.DEF_Employee where d.Id == employee.Id select d).SingleOrDefault();
if (obj == null)
return;

obj.Name = "aaaa";
db.SubmitChanges()
if (!bo)
{
//db = new DataDBDDataContext(ConStr);


throw new Exception("第一次提交失败");
}
}




第二次提交时未执行代码obj.Name = "aaaa";之前 obj.Name已经在第一次提交时改为"aaaa"
我尝试过将db = new DataDBDDataContext(ConStr);重新实例后第二次提交任然失败

DEF_Employee 类有设置主键
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int Id
{
get
{
return this._Id;
}
set
{
if ((this._Id != value))
{
this.OnIdChanging(value);
this.SendPropertyChanging();
this._Id = value;
this.SendPropertyChanged("Id");
this.OnIdChanged();
}
}
}

[最优解释]
Try:
obj.Name = "aaaa";

db.SubmitChanges(ConflictMode.FailOnFirstConflict);
// or
db.SubmitChanges(ConflictMode.ContinueOnConflict);

[其他解释]
CoreProject.Service.EmployeeService.GetSObj.aa(userState.Employee, bo);
方法名称aa写错 对应方法是UpdateName
[其他解释]

public void UpdateName(Employee employee, bool bo)
{
var obj = (from d in db.DEF_Employee where d.Id == employee.Id select d).SingleOrDefault();
if (obj == null)
return;

db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, obj);
db.SubmitChanges();


obj.Name = "aaaa";
db.SubmitChanges()
if (!bo)
{
//db = new DataDBDDataContext(ConStr);
throw new Exception("第一次提交失败");
}
}

这样就解决了,但是性能估计下降很多。

读书人网 >.NET

热点推荐