title: EF 6.1.3 - Cannot insert duplicate key in object
body: <p>I searched on site for solving my problem, but I still haven't solved it.
  I have 2 entities:</p>
  
  <pre><code>public class Article
  {
      [Key]
      public Guid ID { get; set; }
      public Guid? ApprovedBy_ID { get; set; }
      public Guid CreatedBy_ID { get; set; }
      public virtual Profile ApprovedBy { get; set; }
      public virtual Profile CreatedBy { get; set; }
      //New guid for new article
      public Article()
      {
          ID = Guid.NewGuid();
      }
  }
  public class Profile
  {
      [Key]
      public Guid ID { get; set; }
      [Required]
      [StringLength(100)]
      public string FullName { get; set; }
      public Profile()
      {
          ID = Guid.NewGuid();
      }
  }
  </code></pre>
  
  <p>And here is my insert-logic:</p>
  
  <pre><code>    private readonly iContext context;
      public ArticleLogic()
      {
          context = new iContext();
      }
      public IEnumerable&lt;Article&gt; GetAllArticle()
      {
          return context.Articles.Include("Categories").Include("Pictures").Include("ApprovedBy").Include("CreatedBy").Include("Template");
      }
  
      public Article AddArticle(Article article)
      {
              try
              {
                  Profile pf = context.Profiles.First();
                  context.Profiles.Attach(pf);
  
                  Article art = new Article();
                  art.Title = article.Title;
                  art.Description = article.Description;
                  art.Content = article.Content;
                  art.Tag = article.Tag;
                  art.Template = article.Template;
                  //pf has ID = '0816f19c-31c1-4103-8f51-ba422beab1c0' (first row in database)
                  art.CreatedBy = pf;
                  art.CreatedBy_ID = pf.ID;
                  context.Articles.Add(art);
                  context.SaveChanges();
  
                  return article;
              }
              catch (Exception ex)
              {
                  //But ex throw error duplicate ID = '7aa1d064-54ff-47b9-807d-db422fa71f8c' (second row in database)
                  Debug.WriteLine(ex.StackTrace);
  
                  throw ex;
              }
  
      }
  </code></pre>
  
  <p>DB Context:</p>
  
  <pre><code>public myContext()
      : base("name=abc")
  {
      this.Configuration.LazyLoadingEnabled = true;
      this.Configuration.ProxyCreationEnabled = false;
  }
  public virtual DbSet&lt;Category&gt; Categories { get; set; }
  public virtual DbSet&lt;Role&gt; Roles { get; set; }
  public virtual DbSet&lt;Template&gt; Templates { get; set; }
  public virtual DbSet&lt;Article&gt; Articles { get; set; }
  public virtual DbSet&lt;Picture&gt; Pictures { get; set; }
  public virtual DbSet&lt;Profile&gt; Profiles { get; set; }
  
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      //One-to-many: Article - Picture (nullable)
      modelBuilder.Entity&lt;Picture&gt;()
          .HasOptional&lt;Article&gt;(c =&gt; c.Article)
          .WithMany(p =&gt; p.Pictures)
          .HasForeignKey(f =&gt; f.ArticleId);
  
      ////One-to-many: Role - Profile
      modelBuilder.Entity&lt;Profile&gt;()
          .HasRequired&lt;Role&gt;(i =&gt; i.Role)
          .WithMany(i =&gt; i.Profiles)
          .HasForeignKey(f =&gt; f.RoleID);
  
      ///Many-to-many: Article - Category
      modelBuilder.Entity&lt;Article&gt;()
          .HasMany(t =&gt; t.Categories)
          .WithMany(t =&gt; t.Articles)
          .Map(m =&gt; {
                  m.ToTable("ArticleCategories");
                  m.MapLeftKey("ArticleId");
                  m.MapRightKey("CategoryId");
           });
      //
          modelBuilder.Entity&lt;Article&gt;()
              .HasOptional(a =&gt; a.ApprovedBy)
              .WithMany(a =&gt; a.ArticleApprovedBy)
              .HasForeignKey(f=&gt;f.ApprovedBy_ID)
              .WillCascadeOnDelete(false);
  
          modelBuilder.Entity&lt;Article&gt;()
              .HasRequired(a =&gt; a.CreatedBy)
              .WithMany(a =&gt; a.ArticleCreatedBy)
              .HasForeignKey(f =&gt; f.CreatedBy_ID)
              .WillCascadeOnDelete(false);
  
          base.OnModelCreating(modelBuilder);
  }
  </code></pre>
  
  <p>I am using CodeFirst EF 6.1.3 and I am getting an error: Violation of PRIMARY KEY constraint 'PK_Profiles'. Cannot insert duplicate key in object 'dbo.Profiles' like this link <a href="https://stackoverflow.com/questions/9664378/entity-framework-code-first-cannot-insert-duplicate-key-in-object-dbo-t-crpro">Entity Framework Code First - Cannot insert duplicate key in object &#39;dbo.T_CRProviders&#39;</a>? 
  Can anyone help me? Thanks.</p>