并非所有记录都会在 DataContext.SubmitChanges() (Linq to SQL) 上更新
简而言之,一张表中的一条特定记录会更新,而同一表中的多个附加修改不会更新。
theStat
在 .SubmitChanges()
上更新正常
statToIncrease
实例在 SubmitChanges()
上不更新code>
观察 .GetChangeSet()
时,仅执行一次更新。为什么 statToIncrease
的任何实例在修改后都没有更新?我是否必须为每次更新打开一个新的DataDataContext
?
UserStat
表有一个主键。
也许还有一个问题
使用 .Add(double amountToAdd)
之类的方法扩展我的 UserStat 类是否明智?必须打开另一个连接,但这将允许我继续使用仅 LINQ,对吧?
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");
In short, one specific record in one table updates while multiple, additional modifications in the same table do not.
theStat
updates OK upon .SubmitChanges()
statToIncrease
instances do not update upon SubmitChanges()
Upon watching .GetChangeSet()
, only one update is being performed. Why aren't any instances of statToIncrease
updated if modified? Do I have to open a new DataDataContext
for each update?
The UserStat
table has a primary key.
Maybe another question
Would it be wise to extend my UserStat class with a method like .Add(double amountToAdd)
? Another connection would have to be opened, but it would allow me to continue using LINQ-only, right?
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();
}
Update
I found the ugly way of doing this:
db.ExecuteCommand("UPDATE [UserStat] SET [Value] = {0} WHERE [ID] = {1},
statToIncrease.Value + amount, statToIncrease.ID");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定问题到底是什么,但重复使用同一个变量 (
statToIncrease
) 并一次获取一条记录似乎很奇怪。获取对象列表,迭代列表并更新它们,然后调用SubmitChanges
似乎更合理:I am not sure what exactly the problem is, but it does seem weird that you are reusing the same variable (
statToIncrease
) and fetching one record at a time. It seems more reasonable to fetch a list of objects, iterate through the list and update them, and then callSubmitChanges
:尝试使用以下命令显式附加您修改过的实体:
不,你没有。
Try to attach you modified entity explicitly by using :
No, you haven't.
这是你的问题:
这显然会在每次迭代中返回相同的对象。我猜测 theStat 的 ID 为 1,这就是它被更新的原因。
Here's your problem:
this will obviously return the same object on each iteration. And I'm guessing that theStat has an ID of 1, which is why it is updated.