title: EF. Update. Violation of UNIQUE KEY constraint
body: <p>During update getting error:</p>
  
  <pre><code>Violation of UNIQUE KEY constraint 'IX_UniqueClientRequest'. Cannot insert duplicate key in object 'dbo.ClientRequest'. The duplicate key value is (0067f654-a06a-47c1-a13d-d5f35530c11b, -1).
  </code></pre>
  
  <p>The statement has been terminated.</p>
  
  <p>This is the code I'm using:</p>
  
  <pre><code>public class ClientRequestData
  {
      public Guid Id { get; set; }
  
      public ClientData Client { get; set; }
  }
  
  public class ClientRequestDataConfig : EntityTypeConfiguration&lt;ClientRequestData&gt;
  {
      public ClientRequestDataConfig()
      {
          ToTable("ClientRequest");
  
          HasRequired(r =&gt; r.Client)
              .WithMany(c =&gt; c.ClientRequests)
              .Map(m =&gt; m.MapKey("IdClient"))
              .WillCascadeOnDelete(false);
      }
  }
  
  
  public void Save(ClientRequestData request)
      {
          using (DbContext context = new DbContext())
          {
  
              request.Client = context.Clients.FirstOrDefault(c =&gt; c.Id == request.Client.Id);
  
              //Insert
              if (request.Id == Guid.Empty)
              {
                  request.Id = Guid.NewGuid();
                  context.ClientRequests.Add(request);
              }
              //Update
              else
              {
                  ClientRequestData dbRequest = context.ClientRequests.FirstOrDefault(cr =&gt; cr.Id == request.Id);
  
                  dbRequest.Client = request.Client;
              }
              context.SaveChanges();
          }
      }
  </code></pre>
  
  <p>How do i change Client entity state, so it updates refernce only, not trying creatin new client?</p>