簡而言之,一個表中的一個特定記錄會更新,而同一個表中的多個附加修改則不會更新。不是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