title: Violation of PRIMARY KEY constraint Cannot insert duplicate key in object
body: <p>I have a problem I am using lazy loading and <code>virtual</code>, but when using this it generates the following error</p>
  <blockquote>
  <p>Violation of PRIMARY KEY constraint Cannot insert duplicate key in object</p>
  </blockquote>
  <p>How can I solve it?</p>
  <p>These are my classes:</p>
  <pre><code>public class Producto
  {
      [Key]
      public Guid ProductoId { get; set; }
      public Guid InquilinoId { get; set; }
      public string Nombre { get; set; }
      public decimal Precio_Publico { get; set; }
      public string Detalle_producto { get; set; }
      public DateTime? Fecha_publicacion { get; set; }
      public bool Activo { get; set; }    
      public string Img_Producto { get; set; }
      public string CodigoBarras { get; set; }
  
      public virtual Concepto VigenciaPrecio { get; set; }
      public virtual ICollection&lt;Precio&gt; Precios { get; set; }     
  
      public bool Es_Almacenable { get; set; }
      public int Dias_de_Garantia { get; set; }
      public bool Es_Importado { get; set; }
  
      public virtual List&lt;Categoria&gt; Categoria { get; set; } = new List&lt;Categoria&gt;();
      public virtual Impuesto Impuesto { get; set; }
      public virtual Precio Precio { get; set; }
  }
  
  public class Categoria
  {
      public Guid CategoriaId { get; set; }       
      public string Nombre { get; set; }
      public virtual Producto Producto { get; set; }
  }
  
  [HttpPost]
  public async Task&lt;ActionResult&lt;Guid&gt;&gt; Post(Producto producto)
  {
      var user = await userManager.GetUserAsync(HttpContext.User);
      var usercontodo = context.Users.Where(x =&gt; x.Id == user.Id).Include(x =&gt; x.InquilinoActual).FirstOrDefault();
  
      if (!string.IsNullOrWhiteSpace(producto.Img_Producto))
      {
          var imgProducto = Convert.FromBase64String(producto.Img_Producto);
                 
          producto.Img_Producto = await almacenadorDeArchivos.GuardarArchivo(imgProducto, &quot;jpg&quot;, &quot;productos&quot;);
      }
             
      producto.InquilinoId = usercontodo.InquilinoActual.ClienteId;         
  
      context.Add(producto);
      await context.SaveChangesAsync();
  
      return producto.ProductoId;
  }
  </code></pre>
  <p>This is the table <code>categorias</code>:</p>
  <p><a href="https://i.stack.imgur.com/A7Ers.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A7Ers.png" alt="enter image description here" /></a></p>
  <p>This is the table <code>productos</code>:</p>
  <p><a href="https://i.stack.imgur.com/AuXTc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AuXTc.png" alt="enter image description here" /></a></p>
  <p>This is the error:</p>
  <p><a href="https://i.stack.imgur.com/h42Km.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h42Km.png" alt="enter image description here" /></a></p>
  <p><strong>Error</strong>:</p>
  <blockquote>
  <p>Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.</p>
  <p>Microsoft.Data.SqlClient.SqlException (0x80131904): Violation of
  PRIMARY KEY constraint 'PK_Categorias'. Cannot insert duplicate key in
  object 'dbo.Categorias'. The duplicate key value is
  (1737b24b-93a4-4ad9-4d8b-08d85a75ca8e)</p>
  </blockquote>