当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - 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)

网友答案:

The issue is that your Context does not know about 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).

Another possible solution is to copy over all values.

分享给朋友:
您可能感兴趣的文章:
随机阅读: