Search code examples
asp.netsql-serverasp.net-mvcentity-frameworkef-database-first

ASP.NET & SQL Server: won't update but will append/insert


I am using a database-first approach with a custom html helper to get a state of a checkbox using ajax (without using form in the view). I have two tables:

  • Tbl_1 -> Id, state (true or false), name (name of checkbox)
  • Tbl_2 -> Id, user_guid, timestamp, Tbl_1Id (foreign_key)

When I do insert operations, it does without any problem but when I try to update it (based upon the logged in user as it also gets GUID, the table gets appended/inserted with new data).

My controller:

public ActionResult SetState(checkboxstate cbstate)
{
    var UserId = new Guid(System.Security.Claims.ClaimsPrincipal.Current.FindFirst("sub").Value);
    var ent = new StartopDatabaseEntities();

    var cbs = ent.checkboxstates.Where(w => w.Name == "World").FirstOrDefault();

    if (cbs == null) // when there are no records in the database
    { 
        ent.checkboxstates.Add(cbstate); 
        ent.checkboxstateUpdates.SingleOrDefault(c => c.Id == cbstate.Id);

        var cbsOp = new checkboxstateUpdates();
        cbsOp.timestamp = DateTime.Now;
        cbsOp.user_guid = UserId;
        cbstate.checkboxstateUpdates.Add(cbsOp);               
        ent.SaveChanges();               
    } // record in database, update (I've only one user now, so has to update only this one) 
    else
    {
        var cbsOp = new checkboxstateUpdates();   // declare in global
        var chc = new checkboxstate();            // to be declared in global
        var newCbs = ent.checkboxstateUpdates.Include(c => c.checkboxstate).ToList();

        foreach (var u in newCbs)
        {
            if(u.user_guid==UserId && u.CheckboxStateId == u.checkboxstate.Id)
            {
                chc.state = cbstate.state;
                chc.name = cbstate.name;
                ent.checkboxstates.Add(chc);
                cbsOp.Tidspunkt = DateTime.Now;
                cbsOp.OpdateretAfBruger = UserId;
                ent.checkboxstateUpdates.Add(cbsOp);
                ent.SaveChanges();
            }
        }
    }

Can anyone explain please why it's not updating but appending/inserting same data with a new Id (primary key)? I have a simple view where Ajax sends a call to the controller with the state and name of the checkbox. I have also tried

Db.Entry(obj).state = EntityState.Modified 

without any help


Solution

  • You have not written the code for the logic which want to achieve..

    I am not clear on the logic of if block also but the else part can be fixed as following.

    var newCbs = ent.checkboxstateUpdates.Include(c => c.checkboxstate).Where(u.user_guid == UserId).FirstOrDefault();
    
    if(newCbs != null) {
        newCbs.checkboxstate.state = cbstate.state;
        newCbs.checkboxstate.name = cbstate.name;
        newCbs.Tidspunkt = DateTime.Now;
        newCbs.OpdateretAfBruger = UserId;
    
        ent.SaveChanges();
    }