Context.SubmitChanges () does not update despite PK

I am having a problem with the SubmitChanges function provided by the linq implementation for DB in C #. When I run the command, nothing gives an error, but the record is never updated. I raised a question, almost everyone says that the problem is that there is nothing primary key in the table. However, my table has a primary key assigned to it, but SubmitChanges does not happen. To give you an overview of what I'm doing, here is an example:

public void setApproval(string approvalCode, int ID) { using (DatabaseDataContext context = new DatabaseDataContext(DBConnection().getConnectionString())) { myRecord con = getRecord(ID); //Gets the record succesfully, PK field in tact con.ApprovalStatus = approvalCode; context.SubmitChanges(); } } 

As noted above, the record is successfully obtained with all the data in the measure, including the PK field used to identify it. A user with a database connection is given the right to update the table, although here I expect that he will break and complain.

Any ideas? Please let me know if I have not provided sufficient information. Any help is much appreciated!

+4
source share
3 answers

Where does getRecord (ID) get its context to return the record? It is not passed to the method, so I assume that it uses a different context. SubmitChanges () will only see changes for the current context, not the context that uses getRecord (ID).

+4
source

You must get the object through context

 public void setApproval(string approvalCode, int ID) { using (DatabaseDataContext context = new DatabaseDataContext(DBConnection().getConnectionString())) { myRecord con = context.TableName.First(item => item.ID == ID); //Gets the record succesfully, PK field in tact con.ApprovalStatus = approvalCode; context.SubmitChanges(); } } 

When you get an object through Context, it tracks your changes and then saves those changes to SubmitChanges

+5
source

Are you checking if the data was updated using code or using an independent database tool?

If in the code the read code looks suspicious, like the write code: I had similar problems when two applications that did not have a common API exchanged data through the database. The context is not a reflection of what is in the database right now, and no information about updating it will completely fix the problem. If you need to check the database for what was entered by another program or stream, you need to create a new database context object to check the database. The old database context object may still have old data until your last update.

The getRecord function must create a new context or accept the current context that you just edited as a parameter. If it uses a static or class context, it will not have the latest data.

0
source

All Articles