skip to Main Content

I have 2 tables: TableA and TableB in MySQL on which I am trying to do some CRUD operations in .NET. I have written a function to insert a new row in TableA followed by updating a single row in TableB. The incoming request consists of an incommingID parameter. The insert command works as expected but the update command on TableB dosen’t do anything. It doesn’t even give any error. The method is given below:

public int SomeMethod(int incommingID)
{
    MyDBContext db = new MyDBContext();
    TableAModel rowA = new TableAModel
    {
        // ...data to be inserted
    }
    db.TableA.Add(rowA);

    var query = from tableData in db.TableB
                where tableData.id == incommingID
                select tableData;
    var rowToBeUpdated = query.SingleOrDefault();

    if(rowToBeUpdated != null)
    {
        // isAccepted is false for all rows and is to be set true for this particular id but 
        // when updated, the changes don't reflect and the row still has isAccepted as false.
        rowToBeUpdated.isAccepted = true;
        db.SaveChanges();
    }
    return incommindID;
}

I even tried wrapping the code with a try…catch but it didn’t throw any errors. What is the reason that TableA insert is working as expected but TableB update isn’t working?

2

Answers


  1. as far as i know when you make a table in mysql, and set the data type to boolean it transfers to TINYINT. try to set it to 1.

    public int SomeMethod(int incommingID)
    {
       // ...PreviousCode
    
       if(rowToBeUpdated != null)
       {
          db.SaveChanges();
          db.Database.ExecuteSqlCommand("UPDATE `DB Name in thserver`.`TabelB` SET `isAccepted` = 1 WHERE `id` = " + incommingID.ToString() + ";");
       }
       return incommingID;
    }
    
    Login or Signup to reply.
  2. Firstly, you can use try catch blocks to be able to see exception if you have.
    An other option, you may have disabled the ChangeTracker value somewhere.
    Try this;

    > db.ChangeTracker.AutoDetectChangesEnabled = true;
    

    The last one

    Ensure that there are no transactions preventing the changes from being committed. If there is an ongoing transaction that is not committed, changes may not be persisted to the database.

    using (var transaction = db.Database.BeginTransaction())
    {
        try
        {
            // Existing code for insert and update
            transaction.Commit();
        }
        catch (Exception)
        {
            transaction.Rollback();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search