简而言之,一个表中的一个特定记录会更新,而同一个表中的多个附加修改则不会更新。不是DataContext.SubmitChanges()中的所有记录更新()(Linq to SQL)
theStat
在.SubmitChanges()
statToIncrease
情况下也不更新时SubmitChanges()
在看.GetChangeSet()
,正在只进行一次更新更新确定。为什么statToIncrease
更新后没有更新?我必须为每个更新打开一个新的DataDataContext
吗?
该UserStat
表has a primary key。
也许另一个问题
难道是明智致以UserStat类像.Add(double amountToAdd)
的方法?另一个连接将不得不打开,但它可以让我继续使用LINQ-only,对吧?
using (DataDataContext db = new DataDataContext())
{
Random r = new Random();
var theStat = db.UserStats.FirstOrDefault();
// tried scoping `statToIncrease` outside of the for loop
// didn't fix anything
UserStat statToIncrease = null;
for (int i = 0; i < vm.stats.Count(); i++)
{
statToIncrease = db.UserStats.Where(s => s.ID == i)
.FirstOrDefault();
// here, .Property is type float
statToIncrease.Property += r.NextDouble();
// here, .Property is type int
theStat.Property -= 1;
}
// create and insert another object
// this object is inserted correctly
// right here, .ChangeSet() only has one insert and one update
// the update is only for `theStat` and not
// any modifications to `statToIncrease`
db.SubmitChanges();
}
更新
我发现这样做的丑陋的方式:
db.ExecuteCommand("UPDATE [UserStat] SET [Value] = {0} WHERE [ID] = {1},
statToIncrease.Value + amount, statToIncrease.ID");
这很奇怪 - 也许我在实施之前没有足够的考虑它。基本上,'statst'需要减少多个'statToIncrease'操作过程中发现的累积值。我会尽力重新工作并让你知道,谢谢。 – 2011-03-10 16:26:20