title: Entity Framework 6 updates all records with data of the one getting updated
body: <p>I am trying to implement entity framework 6 in my application, but I have a problem performing updates on the records.</p>
  <p>If I have 2 records in the database, lets say:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Id</th>
  <th>Name</th>
  <th>Lastname</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>Jason</td>
  <td>Momoa</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Aqua</td>
  <td>Man</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Then I alter the one with id 1 from &quot;Jason&quot; to &quot;Water&quot; and call the <code>UpdatePerson</code> function, with a new <code>Person</code> object that has the same primary key.</p>
  <p>The result will be:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Id</th>
  <th>Name</th>
  <th>Lastname</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>Water</td>
  <td>Momoa</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Water</td>
  <td>Momoa</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>WHY should that be the result?? I was already looking around for a solution but could not find any clue. Anyone an idea what I am doing wrong?</p>
  <p>As far as I understood the disconnected datacontext I used, can simply update a record with the knowledge of the primary key.</p>
  <p>For reference the page for <a href="https://docs.microsoft.com/en-us/ef/ef6/saving/change-tracking/entity-state#attaching-an-existing-but-modified-entity-to-the-context" rel="nofollow noreferrer">EF6</a></p>
  <p>My code looks like this:</p>
  <pre><code>public class Person
  {
      private int _id = -1;
      private string _name;
      private string _lastname;
  
      public int PersonId { get =&gt; _id; set =&gt; _id = value; }
      [Required]
      [MaxLength(255)]
      public string Name { get =&gt; _name; set =&gt; _name = value; }
      [Required]
      public string Lastname { get =&gt; _lastname; set =&gt; _lastname = value; }
  }
  </code></pre>
  <p><code>DbContext</code>:</p>
  <pre><code>public partial class Model1 : DbContext
  {
      public Model1() : base(&quot;name=entity_test&quot;) { }
  
      public DbSet&lt;Person&gt; People { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
          modelBuilder.Entity&lt;Person&gt;().MapToStoredProcedures();
      }     
  }
  
  public class PersonModel
  {
      public ObservableCollection&lt;Person&gt; GetPeople()
      {
          using (Model1 context = new Model1())
          {
              var list = context.People.AsNoTracking().ToList();
              if (list == null)
                  return null;
              return new ObservableCollection&lt;Person&gt;(list);
          }
      }
  
      public void AddPerson(Person person)
      {
          using (Model1 context = new Model1())
          {
              context.People.Add(person);
              context.SaveChanges();
          }
      }
  
      public void UpdatePerson(Person person)
      {
          using (Model1 context = new Model1())
          {
              context.Entry(person).State = EntityState.Modified;
              context.SaveChanges();
          }
      }
  }
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>Tables were not displayed nicely.</p>
  <p><strong>EDIT 2</strong></p>
  <p>Here the rest of the code and the output of <code>context.Database.Log = s =&gt; Console.WriteLine(s);</code></p>
  <p>Output:</p>
  <pre><code>`Person_Update`
  
  
  -- PersonId: '1' (Type = Int32, IsNullable = false)
  
  -- Name: 'Water' (Type = String, IsNullable = false, Size = 5)
  
  -- Lastname: 'Momoa' (Type = String, IsNullable = false, Size = 5)
  
  -- Executing at 29.10.2021 16:46:05 +02:00
  
  -- Completed in 198 ms with result: 2
  </code></pre>
  <p>Code:</p>
  <pre><code>public class NotifyBase : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
      protected bool SetProperty&lt;T&gt;(ref T field, T newValue, [CallerMemberName] string propertyName = null)
      {
          if (!EqualityComparer&lt;T&gt;.Default.Equals(field, newValue))
          {
              field = newValue;
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
              return true;
          }
          return false;
      }
  }
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
          DataContext = new ViewModel();
  
      }
      private void Button_Click(object sender, RoutedEventArgs e)
      {
          PersonModel model = new PersonModel();
  
          if (DataContext is ViewModel vm)
          {
              vm.AddModifyPerson();
          }
      }
  }
  
  public class ViewModel : NotifyBase
  {
      public ViewModel()
      {
          MiniProfilerEF6.Initialize();
          model = new PersonModel();
  
          using (var db = new Model1())
          {
              // create if not exists
              if (db.Database.CreateIfNotExists())
              {
                  Console.WriteLine();
              }
              People = model.GetPeople();
          }
      }
  
      private PersonModel model;
  
      private ObservableCollection&lt;Person&gt; people = new ObservableCollection&lt;Person&gt;();
      private Person currentPerson = new Person();
  
      public ObservableCollection&lt;Person&gt; People { get =&gt; people; set =&gt; SetProperty(ref people, value); }
      public Person CurrentPerson { get =&gt; currentPerson; set =&gt; SetProperty(ref currentPerson, value); }
  
      public void AddModifyPerson()
      {
          if (CurrentPerson.PersonId == -1)
          {
              model.AddPerson(CurrentPerson);
          }
          else
          {
              model.UpdatePerson(
                  new Person()
                  {
                      PersonId = CurrentPerson.PersonId,
                      Lastname = CurrentPerson.Lastname,
                      Name = CurrentPerson.Name,
                  });
          }
          People = model.GetPeople();
      }
  }
  </code></pre>
  <p><strong>Edit 3</strong></p>
  <p>Code for from miniprofiler</p>
  <pre><code>    public void UpdatePerson(Person person)
      {
          var profiler = MiniProfiler.StartNew(&quot;My Profiler&quot;);
          using (MiniProfiler.Current.Step(&quot;Update_Sql&quot;))
          {
              using (Model1 context = new Model1())
              {
                  context.Entry(person).State = EntityState.Modified;
                  context.SaveChanges();
              }
          }
  
          Console.WriteLine(MiniProfiler.Current.RenderPlainText());
      }
  </code></pre>
  <p><strong>EDIT 4</strong></p>
  <p>output of an update call from mysql.general_log</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>command_type</th>
  <th>argument</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Init DB</td>
  <td>entity_test</td>
  </tr>
  <tr>
  <td>Query</td>
  <td>SET  TRANSACTION ISOLATION LEVEL REPEATABLE READ</td>
  </tr>
  <tr>
  <td>Query</td>
  <td>BEGIN</td>
  </tr>
  <tr>
  <td>Query</td>
  <td>CALL Person_Update(1, 'Jason1', 'Momoa')</td>
  </tr>
  <tr>
  <td>Query</td>
  <td>COMMIT</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>It does look like the update procedure is not performing the where clause. If I run <code>call Person_Update_Custom(1, 'test','tes')</code> in the workbench all rows are updated. Here is the stored procedure:</p>
  <pre class="lang-sql prettyprint-override"><code>    CREATE DEFINER=`root`@`localhost` PROCEDURE `Person_Update`(IN PersonId int,IN Name varchar(255) ,IN Lastname longtext)
      BEGIN 
      UPDATE `People` SET `Name`=Name, `Lastname`=Lastname WHERE `PersonId` = PersonId;
       END
  </code></pre>