简体   繁体   English

记录未更新-Linq SQLite

[英]record not updating - Linq SQLite

given the following code: 给出以下代码:

[HttpPost]
public ActionResult Edit(int id, Table o)
{
    try
    {
        var query = db2.Table.Single(i => i.RecordID == o.RecordID);
        query = o;
        db2.SubmitChanges();

        return RedirectToAction("Index");
    }
    catch { return View(); }

}

The record is not updating as it should. 记录未按预期更新。 It returns without any error, but without performing the desired changes It's strange because if i do: 它返回没有任何错误,但没有执行所需的更改。这很奇怪,因为如果我这样做:

query.propertyToChange = 4;`

or even: 甚至:

query.propertyToChange = o.propertyToChange;

It already works. 它已经可以了。 And: 和:

db2.insertonsubmit(o);

Will just give an error because of a field that should be unique (and that doesn't make any sense because I'm updating an existing row and passing the same key value) so it surely assumes I'm trying to add a new one with the same unique key. 由于一个字段应该是唯一的,只会给出一个错误(并且这没有任何意义,因为我正在更新现有行并传递相同的键值),因此它肯定假设我正在尝试添加一个新字段具有相同的唯一密钥。

Can someone point me out as to why i cannot apply to an existing row the changed values? 有人可以指出为什么我不能将更改后的值应用于现有行吗? (I'm using SQLite if it makes a difference) (如果有区别,我正在使用SQLite)

The issue is that your Context does not know about o . 问题是您的上下文不了解o When you execute the following line: 当您执行以下行时:

query = o;

the query variable is now pointing to an object that your context is unaware of. 查询变量现在指向您的上下文不知道的对象。 Remember that the context tracks objects, not pointers. 请记住,上下文跟踪对象,而不是指针。 You need to find a way to make sure that the context is tracking your object. 您需要找到一种方法来确保上下文正在跟踪您的对象。

If you're using Entity Framework that should be something like db2.Attach(o) . 如果您使用的是Entity Framework,则应该类似于db2.Attach(o)

Another possible solution is to copy over all values. 另一种可能的解决方案是复制所有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM