0
我有一个ParentRef和SortIndex列上具有唯一键的数据库。执行多个LINQ更新以避免唯一键异常
在LINQ中,我想切换两个SortIndex值。我想在一次交易中这样做,所以一次可以有多个用户。我如何使用LINQ一次切换值,所以我的唯一键不会被违反?
var dc = new MyDataContext();
using (TransactionScope trans = new TransactionScope())
{
var pageToBeMoved = dc.Pages.Where(p => p.ID == id).Single();
var pageToBeSwitched = (from p in dc.Pages
where p.ParentRef == pageToBeMoved.ParentRef
where p.SortIndex > pageToBeMoved.SortIndex
orderby p.SortIndex ascending
select p).First();
int tempSortIndex = pageToBeMoved.SortIndex;
pageToBeMoved.SortIndex = pageToBeSwitched.SortIndex;
pageToBeSwitched.SortIndex = tempSortIndex;
dc.SubmitChanges();
trans.Complete();
}
感谢您的回复,我已经尝试设置的第一个0。但是,这并不工作。我正在考虑删除我的唯一密钥,并依靠我的代码与交易一起工作。 – 2009-09-20 19:59:11
它不起作用的方式?你有重大违规行为吗? – codeulike 2009-09-20 20:23:19
是的,我愿意。正如我的原始代码一样。我想这可能与交易有关。 – 2009-09-23 15:28:56