title: How update object with relation data in EntityFramework Core
body: <p>I have used the <code>Entity Framework Core</code> and update some object with relation to another object.</p>
  
  <p>I have a following entities:</p>
  
  <p>Client:</p>
  
  <pre><code>public class Client
  {
      public int Id { get; set; }
  
      public string ClientId { get; set; }
  
      public string ClientName { get; set; }
  
      public List&lt;ClientScope&gt; Scopes { get; set; }
  }
  </code></pre>
  
  <p>ClientScope:</p>
  
  <pre><code>public class ClientScope
    {
      public int Id { get; set; }
  
      public string Scope { get; set; }
  
      public Client Client { get; set; }
    }
  </code></pre>
  
  <p>OnModelCreating:</p>
  
  <pre><code>modelBuilder.Entity&lt;Client&gt;((Action&lt;EntityTypeBuilder&lt;Client&gt;&gt;) (client =&gt;
        {
          client.ToTable&lt;Client&gt;(storeOptions.Client);
          client.HasKey((Expression&lt;Func&lt;Client, object&gt;&gt;) (x =&gt; (object) x.Id));
          client.Property&lt;string&gt;((Expression&lt;Func&lt;Client, string&gt;&gt;) (x =&gt; x.ClientId)).HasMaxLength(200).IsRequired(true);
          client.HasIndex((Expression&lt;Func&lt;Client, object&gt;&gt;) (x =&gt; x.ClientId)).IsUnique(true);
          client.HasMany&lt;ClientScope&gt;((Expression&lt;Func&lt;Client, IEnumerable&lt;ClientScope&gt;&gt;&gt;) (x =&gt; x.AllowedScopes)).WithOne((Expression&lt;Func&lt;ClientScope, Client&gt;&gt;) (x =&gt; x.Client)).IsRequired(true).OnDelete(DeleteBehavior.Cascade);
        }));
  
  modelBuilder.Entity&lt;ClientScope&gt;((Action&lt;EntityTypeBuilder&lt;ClientScope&gt;&gt;) (scope =&gt;
        {
          scope.ToTable&lt;ClientScope&gt;(storeOptions.ClientScopes);
          scope.Property&lt;string&gt;((Expression&lt;Func&lt;ClientScope, string&gt;&gt;) (x =&gt; x.Scope)).HasMaxLength(200).IsRequired(true);
        }));
  </code></pre>
  
  <p>I would like update the <code>ClientScope</code> for specific <code>Client.Id</code> for example Id = 1.</p>
  
  <p>I have tried use this way:</p>
  
  <pre><code>public void UpdateClientScope(ClientScope scope){
  
  _dbContext.ClientScope.Update(scope);
  _dbContext.SaveChanges();
  }
  
  var scope = new ClientScope() { Client = new Client{Id = 1}, Id = 1, Scope = "Test Scope" }
  UpdateClientScope(scope);
  </code></pre>
  
  <p>But this way try to update the <code>Client</code> as well. I want to update only ClientScope and specify ClientId which it is stored on my form. </p>
  
  <p>What is the best way how update <code>ClientScope</code> above?</p>
  
  <p>I have tried to implement the <code>BaseRepository</code> which I want to implement for every entity something like this:</p>
  
  <pre><code>public class BaseRepository&lt;TDbContext, TEntity, TPrimaryKey&gt; : IBaseRepository&lt;TDbContext, TEntity, TPrimaryKey&gt;
          where TEntity : class
          where TDbContext : DbContext
  {
   public virtual DbSet&lt;TEntity&gt; Table =&gt; _dbContext.Set&lt;TEntity&gt;();
  
  private readonly TDbContext _dbContext;
  
  public BaseRepository(TDbContext dbContext)
          {
              _dbContext = dbContext;
          }
  
  public virtual async Task&lt;TEntity&gt; UpdateAsync(TEntity entity)
          {
              Table.Update(entity);
  
              await _dbContext.SaveChangesAsync();
  
              return entity;
          }
  }
  </code></pre>
  
  <p>But I don't know - How correctly specify the <code>update</code> method for entities like this?</p>
  
  <p>Thank you for any suggestion.</p>