Whats the regular path to update a property? Select it by id, bring it all from DB, update a property and then call SaveChanges... You can do better even if you're confined to EntityFramework

How To Update A Single Property Of An Entity In Entity Framework

Another day in the office, another entity needs to be updated.

So, your boss needs it like now and you wanna get home early. No problemo, EntityFramework's got your back!

You just open a database context, search an entity, update a field, and call SaveChanges. We're ready to go home to Starla and the kids!!!

using(var db = new DbContext())
{
    var user = db.Users.Find(userId);
    user.Name = "New Name";
    db.SaveChanges();
}

Or maybe not. I mean it works but there's something weird, something that just feels wrong about it. What if "User" is a very long table? What if "User" has several "navigation properties". Well, you just hit one of the biggest problems you'll find while using an ORM.

Don't get me wrong, I love ORMs. I kindda hate SQL (I mean, it's great, it's just that, in the context of a programming language it feels so foreign and lacking of integration, that's where EntityFramework shines big time) and I love Entity Framework's POCOs. It's just that, yeah, there are problems that get a little bit verbose in an ORM, specially when you need granularized control. In this case, we know the id of our the entity to update, and we know which field we want to update, as well as the info to update. Why on earth would we want to bring the whole entity through the wire into memory before doing such thing?

The answer is: there's no reason . ORMs tend to fail here. Of course, you could fall back to plain good ol' SQL, but that, like, totally blows the purpose of using an ORM. In my case, it totally blows type safety which for me is precious...

Gimme my type safety back!!! Dont worry. We're gonna use a little trick here. We're not going to take the entity from database. We're going to create an empty entity, attach it to our DB context (this is like fooling our ORM to think we took it from database), asign the desired property to update, manually mark it updated and then call SaveChanges. Don't forget to disable validation or you just won't be able to save your incomplete entity!!!

using(var db = new DbContext())
{
    db.Configuration.ValidateOnSaveEnabled = false;
    try
    {
        var user = new User { id = userId, Name = "New Name" };
        db.Users.Attach(user);
        db.Entry(user).Property(u=> u.Name).IsModified = true;
        db.SaveChanges();
    }
    finally
    {
        db.Configuration.ValidateOnSaveEnabled = true;
    }
}

Yeah I know, a little bit way too much verbose than I would expect, but hey is not that bad. At least I still have my type safety.

Happy Coding!

Posted by: fabzter
Last revised: 18 Jul, 2012 01:28 AM History

Comments

Chetan
Chetan
26 Aug, 2015 07:19 PM

Thank You buddy..

You are right..its must to disable validation

db.Configuration.ValidateOnSaveEnabled = false;

BedUnunk
BedUnunk
04 Dec, 2016 07:31 AM

lonazep 0.5 side effects clanazepam . order clonazepam no prescription rx clonazepam clonazepam picture clonazepam purchase clonazepam 0.5 mg tablet clonazepam 0.5 . clonotril 0.5 mg side effects clonazepam dosing colanapins side effects rivotril clonazepam 2mg. clonazepam en espanol side effect of clonazepam clonazepam 2mg tablets clonazepam tablet .

BerGrest
BerGrest
09 Dec, 2016 06:18 AM

can clonazepam cause seizures clonazepam canada . clonazepam for insomnia clonazepam generic name clonazepam anti anxiety medication clonazepam doses available ativan online klonapin . side effects for clonazepam clonazepam effects clonazepam sleep aid buy clonazepam. taking clonazepam clonazepam 0.5 mg side effects clonazepam blue pill clonazepam dry mouth .

Your Comments

Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.

Preview