3

I'm trying to update a record using EntityFramework 6 by attaching a disconnected entity. I want to update a single boolean field to false but it doesn't work. I have used sql server profiler and EF doesn't generate an update statement when calling SaveChanges on the context. But if i set the value to true, it works. Example:

This doesn't work:

 private void UpdateUser()
 {
    var user = new User { ID = 5 };
    this.Context.Users.Attach(user);

    user.Locked = false;
    this.Context.SaveChanges();
 }

This works:

 private void UpdateUser()
 {
    var user = new User { ID = 5 };
    this.Context.Users.Attach(user);

    user.Locked = true;
    this.Context.SaveChanges();
 }

I can fix by marking the property as modified like this:

this.Context.Entry(user).Property(e => e.Locked).IsModified = true;

But don't understand why does it work if the value is true and not if the value is false. There is a similar issue when trying to set a field to null.

There's probably something wrong with the entity data model or database but can't figure out what.

SzilardD
  • 1,611
  • 2
  • 22
  • 40

3 Answers3

4

Default value for Boolean is false, so setting to false after attaching won't flag the property as changed.

You can load the entity first, then do your modifications then save changes.

var user = db.Users.Find(5);
user.Locked = false;
db.SaveChanges();

Or you can attach the entity with the field you want to update set to a different value for Boolean fields

var user = new User { Id = 5, Locked = true; };
db.Users.Attach(user);
user.Locked = false;
db.SaveChanges();
Alaa Masoud
  • 7,085
  • 3
  • 39
  • 57
  • var user = new User { Id = 5, Locked = !valueToSet; }; db.Users.Attach(user); user.Locked = valueToSet; db.SaveChanges(); You saved my day. – David Chelliah Jun 09 '16 at 21:08
1

False is the default value of boolean field, and, thus, setting false again will not report to change tracker that the property was changed. as a result it is not part of insert/update statement. If it is database first approach, your property should look similar to this(see the setter):

public global::System.Boolean Locked
{
            get
            {
                return _Locked;
            }
            set
            {
                if (_Locked != value)
                {
                    OnLockedChanging(value);
                    ReportPropertyChanging("Locked");
                    _Locked = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("Locked");
                    OnLockedChanged();
                }
            }
}
Andrew
  • 3,648
  • 1
  • 15
  • 29
-1

Apologies for the same question here:

     var user = new User { Id = 5, Locked = true; };
     db.Users.Attach(user);
     user.Locked = false;
     db.SaveChanges();

above solutions is not worked for me, but below code is worked for me.

     var user = new User { Id = 5, Locked = true; };
     db.Users.Attach(user);
     db.SaveChanges();
     user.Locked = false;
     db.SaveChanges();

But calling SaveChanges() two times looks not good. if there any other solutions suggests me. or what is exact root cause for the problem.

Dev Baabz
  • 1
  • 1