Adapter: Cannot save or delete entities

Posts   
 
    
ABOH
User
Posts: 148
Joined: 06-Sep-2011
# Posted on: 17-Aug-2012 08:20:55   

Hello,

I have a simple MVC applicatation and am adapting it to use LLBLGEN rather than EF. I can create new entities without any issues. When I edit an entity, and it arrives in the following method for saving, everything is successful, but when I look in the database, the record has not been updated. I have the same issue when trying to delete records, too. The method is successful, but the database is not updated. I would really appreciate a hint as to what I am doing wrong. I am using an adapter...

Thank you for your suggestions, Mike

        [HttpPost]
        public ActionResult Edit(long id, CertificateEntity certificate)
        {
            DataAccessAdapter adapter = null;

            if (certificate == null)
            {
                throw new ArgumentNullException("certificate");
            }

            try
            {           
                certificate.IsNew = false;

                using (adapter = new DataAccessAdapter())
                {
                    adapter.StartTransaction(System.Data.IsolationLevel.Serializable, "Edit");

                    if (!adapter.SaveEntity(certificate))
                    {
                        // Failed to save the certificate
                        throw new Exception();
                    }

                    // Commit our edits
                    adapter.Commit();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                // Abort and roll back the transaction
                adapter.Rollback();

                // Bubble up exception
                throw;
            }
            finally
            {
                if (adapter != null)
                {
                    adapter.Dispose();
                    adapter = null;
                }
            }
        }
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 17-Aug-2012 10:26:59   

You set IsNew to false, but you don't mark any fields as changed, so the entity's IsDirty flag is false (it's a flag on the fields: certificate.Fields.IsDirty)

I don't know how you do change tracking, but research we did among web devs resulted in that most people using MVC refetch the entity, then set the properties of the fetched entity with the values from the entity to persist and then save the fetched entity again (so any changes are tracked properly).

You can also do:

bool isDirty = false;
foreach(var field in certificate.Fields)
{
    if(field.IsPrimaryKey || field.IsReadOnly)
    {
        continue;
    }
    field.IsChanged = true;
    isDirty = true;
}

certificate.Fields.IsDirty = isDirty;

and then save it (you can make this a generic method of course)

Not tested but you get the idea. This might mark fields which aren't to be updated as updated, and it will always update your entity, even if nothing changed.

Frans Bouma | Lead developer LLBLGen Pro
ABOH
User
Posts: 148
Joined: 06-Sep-2011
# Posted on: 17-Aug-2012 23:34:49   

Hi Otis,

Thank you very much for your response and suggestions! I assumed that the View in which I was editing an entity would mark the changed fields due to editing them, but it seems that this is not the case, which is why I was confused! simple_smile I will experiment with the strategy that you've mentioned and hopefully it will resolve things for me.

Thank you again for your help and have a great weekend!

Mike