title: EntityFramework core - Update a collection of data without selecting the entities
body: <p>How would you Upsert without select? the upsert would be a collection of entities received by a method which contains DTOs that may not be available in the database so you can NOT use attach range for example.
  One way theoretically is to load the ExistingData partially with a select like <code>dbContext.People.Where(x =&gt; x exists in requested collection).Select(x =&gt; new Person { Id = x.Id, State = x.State }).ToList()</code> which just loads a part of the entity and not the heavy parts. But here if you update one of these returned entityItems from this collection it will not update because of the <code>new Person</code> its not tracking it and you also cannot say <code>dbContext.Entry&lt;Person&gt;(person).State = Modified</code> because it will throw an error and will tell you that ef core is already &quot;Tracking&quot; it.
  So what to do.
  One way would be to detach all of them from the <code>ChangeTracker</code> and then do the state change and it will do the update but not just on one field even if you say <code>dbContext.Entry&lt;Person&gt;(person).Property(x =&gt; x.State).Modified = true</code>. It will overwrite every fields that you haven't read from the database to their default value and it will make a mess in the database.
  The other way would be to read the <code>ChangeTracker</code> entries and update them but it will also overwrite and it will consider like everything is chanaged.
  So techinically I don't know how ef core can create the following SQL,</p>
  <pre><code>update People set state = 'Approved' where state != 'Approved'
  </code></pre>
  <p>without updating anything else. or loading the person first completely.</p>
  <p>The reason for not loading your data is that you may want to update like <code>14000</code> records and those records are really heavy to load because they contain byte[] and have images stored on them for example.</p>
  <p>BTW the lack of friendly documentation on EFCore is a disaster compare to Laravel. Recently it has cost us the loss of a huge amount of data.</p>
  <p>btw, the examples like the code below will <strong>NOT</strong> work for us because they are updating one field which they know that it exists in database. But we are trying to upsert a collection which some of those DTOs may not be available in the database.</p>
  <pre><code>try
  {
      using (var db = new dbContext())
      {
          // Create new stub with correct id and attach to context.
          var entity = new myEntity { PageID = pageid };
          db.Pages.Attach(entity);
  
          // Now the entity is being tracked by EF, update required properties.
          entity.Title = &quot;new title&quot;;
          entity.Url = &quot;new-url&quot;;
          
          // EF knows only to update the propeties specified above.
          db.SaveChanges();
      }
  }
  catch (DataException)
  {
      // process exception
  }
  </code></pre>
  <p><strong>Edit:</strong> The used <code>ef core</code> version is <code>@3.1.9</code></p>