并非所有记录都会在 DataContext.SubmitChanges() (Linq to SQL) 上更新

发布于 2024-10-20 12:11:55 字数 1889 浏览 3 评论 0原文

简而言之,一张表中的一条特定记录会更新,而同一表中的多个附加修改不会更新。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好倦 2024-10-27 12:11:55

我不确定问题到底是什么,但重复使用同一个变量 (statToIncrease) 并一次获取一条记录似乎很奇怪。获取对象列表,迭代列表并更新它们,然后调用 SubmitChanges 似乎更合理:

using (DataDataContext db = new DataDataContext())
{
    var r = new Random();

    UserStat theStat = db.UserStats.FirstOrDefault();

    List<UserStat> statsToIncrease = 
                               db.
                               UserStats.
                               Where(s => s.ID >= 0 && s.ID < vm.stats.Count()).
                               ToList();

    foreach (UserStat statToIncrease in statsToIncrease)
    {
        statToIncrease.Property += r.NextDouble();
        theStat.Property -= 1;
    }

    db.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 call SubmitChanges:

using (DataDataContext db = new DataDataContext())
{
    var r = new Random();

    UserStat theStat = db.UserStats.FirstOrDefault();

    List<UserStat> statsToIncrease = 
                               db.
                               UserStats.
                               Where(s => s.ID >= 0 && s.ID < vm.stats.Count()).
                               ToList();

    foreach (UserStat statToIncrease in statsToIncrease)
    {
        statToIncrease.Property += r.NextDouble();
        theStat.Property -= 1;
    }

    db.SubmitChanges();
}
梦醒时光 2024-10-27 12:11:55

尝试使用以下命令显式附加您修改过的实体:

db.UserStats.Attach(statToIncrease, true);
...
db.SubmitChanges();

我必须打开一个新的吗?
每次更新的DataDataContext?

不,你没有。

Try to attach you modified entity explicitly by using :

db.UserStats.Attach(statToIncrease, true);
...
db.SubmitChanges();

Do I have to open a new
DataDataContext for each update?

No, you haven't.

年华零落成诗 2024-10-27 12:11:55

这是你的问题:

.Where(s => s.ID == 1).FirstOrDefault();

这显然会在每次迭代中返回相同的对象。我猜测 theStat 的 ID 为 1,这就是它被更新的原因。

Here's your problem:

.Where(s => s.ID == 1).FirstOrDefault();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文