title: Disconnected Entities in EF Core
body: <p>I have been looking to log changes on entities in a disconnected scenario. </p>
  
  <p><strong>Original Problem:</strong> I call <code>DbContext.Update(Entity)</code> on an updated complex entity and even though nothing has changed, everything is marked as Changed by the ChangeTracker and my concurrency <strong>RowVersion</strong> column counts up.</p>
  
  <p><strong>POST User with no changes</strong></p>
  
  <pre><code>"userSubPermissions": [],
  "fidAdresseNavigation": {
      "idAdresse": 1,
      "fidPlzOrt": 3,
      "strasse": "Gerold Str.",
      "hausnr": "45",
      "rowVersion": 10,
      "isDeleted": 0,
      "fidPlzOrtNavigation": {
          "idPlzOrt": 3,
          "plz": "52062",
          "ort": "Aachen",
          "rowVersion": 9,
          "isDeleted": 0
      }
  },
  "idUser": 35,
  "fidAnrede": null,
  "fidAdresse": 1,
  "fidAspnetuser": "a7ab78be-859f-4735-acd1-f06cd832be7e",
  "vorname": "Max",
  "nachmname": "Leckermann",
  "eMail": "kunde@leasing.de",
  "rowVersion": 11,
  "isDeleted": 0
  </code></pre>
  
  <p><strong>POST Returned user</strong></p>
  
  <pre><code>"userSubPermissions": [],
  "fidAdresseNavigation": {
      "idAdresse": 1,
      "fidPlzOrt": 3,
      "strasse": "GeroldPenis Str.",
      "hausnr": "45",
      "rowVersion": 11,
      "isDeleted": 0,
      "fidPlzOrtNavigation": {
          "idPlzOrt": 3,
          "plz": "52062",
          "ort": "Aachen",
          "rowVersion": 10,
          "isDeleted": 0
      }
  },
  "idUser": 35,
  "fidAnrede": null,
  "fidAdresse": 1,
  "fidAspnetuser": "a7ab78be-859f-4735-acd1-f06cd832be7e",
  "vorname": "Max",
  "nachmname": "Leckermann",
  "eMail": "kunde@leasing.de",
  "rowVersion": 12,
  "isDeleted": 0
  </code></pre>
  
  <p>The RowVersion logic is in the <strong>DBContext</strong> and only changes the row version when the Entity State is Modified.</p>
  
  <pre><code>foreach (var entity in ChangeTracker.Entries().Where(e =&gt; e.State == EntityState.Modified))
          {
              var saveEntity = entity.Entity as ISavingChanges;
              saveEntity.OnSavingChanges();
          }
  </code></pre>
  
  <p>From my investigation over the past 2 days there seem to be two alternatives for EF Core. </p>
  
  <p>1.- As mentioned on this and many similar posts you can use the TrackGraph <a href="https://www.mikesdotnetting.com/article/303/entity-framework-core-trackgraph-for-disconnected-data" rel="nofollow noreferrer">https://www.mikesdotnetting.com/article/303/entity-framework-core-trackgraph-for-disconnected-data</a></p>
  
  <p><strong>Problem:</strong> This solution marks the entire entity as changed when the ID of the entity is present even though nothing has changed. I get the exact same result as described above, the <strong>RowVersion</strong> counts up meaning my Entities an all of its Properties have been marked as Modified.</p>
  
  <p>2.- As mentioned in this post 
  <a href="https://blog.tonysneed.com/2017/10/01/trackable-entities-for-ef-core/" rel="nofollow noreferrer">https://blog.tonysneed.com/2017/10/01/trackable-entities-for-ef-core/</a> you can download a NuGet package and implement the <strong>IMergeable</strong> and the <strong>ITrackable</strong>
  interfaces on my Entities. </p>
  
  <p><strong>Problem:</strong> The Client needs to track the changes in the Model and pass them to the API, which I want to avoid since Angular does not seem to offer a good solution for this.</p>
  
  <p>3.- There is another solution in the book <strong>Programming Entity Framework</strong> on how EF 6 handles disconnected scenarios, by recording original values <strong>Chapter 4: Recording Original Values</strong>. 
  When an Update comes in, the context reads the entity from the DB and then compares the incoming Entity with the DB Entity to see if any changes have been made on the properties and then marks them as Modified. </p>
  
  <p><a href="https://www.oreilly.com/library/view/programming-entity-framework/9781449331825/ch04.html" rel="nofollow noreferrer">https://www.oreilly.com/library/view/programming-entity-framework/9781449331825/ch04.html</a></p>
  
  <p><strong>Problem:</strong> Some of the code described in the book is not implementable in EF Core one example is. </p>
  
  <pre><code>((IObjectContextAdapter)this).ObjectContext
  .ObjectMaterialized += (sender, args) =&gt;
  {
    var entity = args.Entity as IObjectWithState;
    if (entity != null)
    {
      entity.State = State.Unchanged;
  
      entity.OriginalValues =
        BuildOriginalValues(this.Entry(entity).OriginalValues);
    }
  };
  </code></pre>
  
  <p>There must be a way to implement this clever solution of handling changes on the API instead of the Client in Disconnected scenarios, otherwise, this might be a very useful Feature to have in EF Core since most Developers are using it for Disconnected scenarios. </p>