2

Here i have a method in ASP.NET MVC. What i am doing is to update single column checking every column of table is not null. If null then IsModified property changing to false. I have to write statement for every column.

My Sample Method -

public int ServicesEdit(helplineservice _helplineservice)
        {
            int result = 0;
            try
            {
                db.Entry(_helplineservice).State = EntityState.Modified;
                if (string.IsNullOrEmpty(_helplineservice.description))
                {
                    db.Entry(_helplineservice).Property(p => p.description).IsModified = false;
                }
                else
                {
                    db.Entry(_helplineservice).Property(p => p.description).IsModified = true;
                }
                if (string.IsNullOrEmpty(_helplineservice.title))
                {
                    db.Entry(_helplineservice).Property(p => p.title).IsModified = false;
                }
                else
                {
                    db.Entry(_helplineservice).Property(p => p.title).IsModified = true;
                }
                if (string.IsNullOrEmpty(_helplineservice.contactnumber))
                {
                    db.Entry(_helplineservice).Property(p => p.contactnumber).IsModified = false;
                }
                else
                {
                    db.Entry(_helplineservice).Property(p => p.contactnumber).IsModified = true;
                }
                //if (string.IsNullOrEmpty(_helplineservice.active.ToString()))
                //{
                //    db.Entry(_helplineservice).Property(p => p.active).IsModified = false;
                //}
                //else
                //{
                //    db.Entry(_helplineservice).Property(p => p.active).IsModified = true;
                //}
                db.SaveChanges();
                result = 1;
            }
            catch (Exception ex)
            {
                result = 0;
            }
            return result;
        }

Calling Above Method -

helplineservice _helplineservice = new helplineservice();
_helplineservice.helplineid =sectionid;
_helplineservice.allowedtoapp = allow;
result = _ftwCommonMethods.ServicesEdit(_helplineservice);

This code not look logical i think. Tell me better way to do this. How Can i Update Single Column of Table by not writing this much code? Thanks in Advance.

Atul
  • 440
  • 8
  • 24
  • 1
    At the very least you can rewrite the `if` statements as `?:` expressions, e.g. `db.Entry(_helplineservice).Property(p => p.contactnumber).IsModified = !string.IsNullOrEmpty(_helplineservice.contactnumber);` – DavidG Oct 27 '15 at 14:16
  • Do you want to save the change of a single property of your entity to the database? – Domysee Oct 27 '15 at 14:19
  • yes @Domysee. i want to change only single property. but while i am doing this other properties are saving null if they are nullable without checking IsModified. – Atul Oct 27 '15 at 14:21
  • @DavidG yes i know it can be like you said. but if i am having 50 properties/columns then? i want to avoid repetitive code. Is there any other way to do this? – Atul Oct 27 '15 at 14:22
  • How do you load the entity? Normally EntityFramework loads all properties, so there are no null values (except if they are in the database) – Domysee Oct 27 '15 at 14:25
  • added in details @Domysee. i just want to update single property how you would do? – Atul Oct 27 '15 at 14:29
  • This is a common issue with EF in disconnected environments. Assuming your single column change is coming back from the web, your best bet is to either return the entire entity from the web client or just return the single updated value, load the entity from the database, set the single value to the new value, then save it back to the database. – Bradford Dillon Oct 27 '15 at 14:37
  • You're creating a new class `new helplineservice()`, then assigning 2 properties (`helplineid` and `allowedtoapp`) and saving it, so why are you checking all those other fields? – markpsmith Oct 27 '15 at 14:40
  • Thanks guys for help! – Atul Oct 28 '15 at 02:58

1 Answers1

1

You can avoid all of the checking by loading the entity you want to update first.

var context = new DbContext();

// Load entity via whatever Id parameter you have.
var entityToUpdate = context.Set<Type>().FirstOrDefault(x => x.Id == idToUpdate);

if(entityToUpdate != null)
{
    entityToUpdate.Value1 = newValue1;
    entityToUpdate.Value2 = newValue2;

    context.SaveChanges();
}

Only Value1 and Value2 will be updated. All other existing values will remain unchanged.

In your case, what you are doing is creating a new empty entity, then setting its Key to something that already exists in the database. When you attach that entity to the context, EF has no choice but to assume that all the values in that entity are the new updated values. This is standard behavior for EF.

Bradford Dillon
  • 1,750
  • 13
  • 24