8,497
社区成员
发帖
与我相关
我的任务
分享
public bool DelCategory(int id)
{
bool ret = false;
try
{
List<int> list = new List<int>();
if (!list.Contains(id))
{
list.Add(id);
}
list = GetKey(id, list);
var x = from s in dbLinq.MaterialAttributes where list.Contains(s.ID) select s ;
foreach (var n in x )
{
dbLinq.MaterialAttributes.DeleteOnSubmit(n);
try
{
dbLinq.SubmitChanges();
}
catch (System.Data.Linq.ChangeConflictException)
{
foreach (System.Data.Linq.ObjectChangeConflict occ in dbLinq.ChangeConflicts)
{
//以下是解决冲突的三种方法,选一种即可
// 使用当前数据库中的值,覆盖Linq缓存中实体对象的值
// occ.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
// 使用Linq缓存中实体对象的值,覆盖当前数据库中的值
occ.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
// 只更新实体对象中改变的字段的值,其他的保留不变
//occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
}
// 这个地方要注意,Catch方法中,我们前面只是指明了怎样来解决冲突,这个地方还需要再次提交更新,这样的话,值 //才会提交到数据库。
dbLinq.SubmitChanges();
}
ret = true;
}
}
catch (Exception ex)
{
ret = false;
throw new ApplicationException(ex.Message);
}
finally {
}
return ret;
}