Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context.SubmitChanges() not updating despite having a PK

I am having an issue with the SubmitChanges function provided by the linq to DB implementation in C#. When I run the command, nothing throws an error but the record never gets updated. I have looked up the issue almost everyone says that it is in issue with the table nothing a primary key. However my table has a primary key assigned to it and yet SubmitChanges does not happen. To give you an overview of what I am executing, I here is a sample:

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 commented above, the record is successfully obtained with all the data in tact, including the PK field used to identify it. The database connection user is given the rights to update the table, though here I would expect it to break and complain.

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

like image 940
Serguei Fedorov Avatar asked Jan 13 '23 22:01

Serguei Fedorov


1 Answers

You should 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 the object via Context, it keep track of changes you make and then it save those changes on SubmitChanges

like image 93
Haris Hasan Avatar answered Jan 30 '23 23:01

Haris Hasan