title: Entity Framework 6 inserting duplicate values
body: <p>I have following two entities:</p>
  
  <pre><code>public class Artist
  {
      [Key]
      public string ArtistId { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;Genre&gt; Genres { get; set; }
  }
  
  public class Genre
  {
      [Key]
      public int GenreId { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;Artist&gt; Artist { get; set; }
  }
  </code></pre>
  
  <p>In my program I create some artists and want to save them:</p>
  
  <pre><code>using (var context = new ArtistContext())
  {
      var artists = _fullArtists.Select(x =&gt; x.Artist);
  
      foreach (var artist in artists)
      {
          context.Artists.AddOrUpdate(artist);
      }
  
      context.SaveChanges();
  }
  </code></pre>
  
  <p>Entity Framework correctly created the three tables:</p>
  
  <p><kbd>Artist (ArtistId, Name)</kbd><br />
  <kbd>Genre (GenreId, Name)</kbd><br />
  <kbd>ArtistGenre (ArtistId, GenreId)</kbd></p>
  
  <p>But unfortunately, when my artist sample data look like this:</p>
  
  <pre><code>var a1 = new Artist { Name = "a1" };
  a1.Genres.Add(new Genre { Name="rock"});
  var a2 = new Artist { Name = "a2" };
  a2.Genres.Add(new Genre { Name="rock"});
  </code></pre>
  
  <p>It will create 2 records in table <code>Genre</code>:</p>
  
  <p><kbd>Id</kbd><kbd>Name</kbd><br />
  <kbd>1&nbsp;</kbd><kbd>rock&nbsp;&nbsp;&nbsp;</kbd><br />
  <kbd>2&nbsp;</kbd><kbd>rock&nbsp;&nbsp;&nbsp;</kbd></p>
  
  <p>instead of creating it once and then reuse it.</p>
  
  <ul>
  <li>Do you know if this is a configuration problem or how to tell EF to not insert duplicates and reuse existing instead?</li>
  </ul>
  
  <p>Thanks in advance</p>
  
  <hr>
  
  <p><strong>Edit:</strong> Unfortunately, the solution of Sergey Berezovskiy didn't work (or probably i did something wrong :D)</p>
  
  <p>What I have now is the following:</p>
  
  <pre><code>using (var workUnit = WorkUnitFactory.CreateWorkUnit())
  {
      var snapShot = new Snapshot { Date = DateTime.Now.Date };
  
       //ICollection&lt;Genre&gt; genres = _fullArtists.Select(x =&gt; x.ToArtist(snapShot)).SelectMany(x =&gt; x.Genres).Distinct(new GenreComparer()).ToList();
       //workUnit.Repository&lt;IGenreRepository&gt;().InsertEntities(genres);
       //workUnit.Commit();
  
       var artists = _fullArtists.Select(x =&gt; x.ToArtist(snapShot)).ToList();
  
       workUnit.Repository&lt;IArtistRepository&gt;().InsertEntities(artists);
       workUnit.Commit();
  }
  </code></pre>
  
  <p>ArtistExtensions.cs</p>
  
  <pre><code>    public static class ArtistExtensions
      {
          public static Artist ToArtist(this FullArtistWrapper value, Snapshot snapShot)
          {
              if (value == null) { throw new ArgumentNullException(); }
  
              var artist = new Artist
              {
                  ArtistId = value.Id,
                  Name = value.Name
              };
  
              var genres = value.Genres.Select(x =&gt; x.ToGenre()).ToList();
              artist.Genres.AddRange(genres);
  
              return artist;
          }
      }
  </code></pre>
  
  <p>GenreExtensions.cs</p>
  
  <pre><code>public static class GenreExtensions
      {
          public static Genre ToGenre(this string value)
          {
              using (var workUnit = WorkUnitFactory.CreateWorkUnit())
              {
                  return workUnit.Repository&lt;IGenreRepository&gt;().GetGenres().FirstOrDefault(x =&gt; x.Name == value) ??
                              new Genre {Name = value};
              }
          }
      }
  </code></pre>
  
  <p>Unfortunately, EF still inserts duplicate <code>Genres</code> in the database.</p>
  
  <p><code>InsertEntities(...)</code> is this:</p>
  
  <pre><code>public void InsertEntities&lt;TPersistentEntity&gt;(ICollection&lt;TPersistentEntity&gt; persistentEntitiesToBeInserted) where TPersistentEntity : PersistenceEntity
      {
          persistentEntitiesToBeInserted.ForEach(this.Add);
      }
  
  public void Add(PersistenceEntity entity)
      {
          var dbSet = this.Context.Set(entity.GetType());
          dbSet.Add(entity);
      }
  </code></pre>
  
  <ul>
  <li>Did I misunderstood the answer of Sergey or what could be another reason that EF still inserts duplicates?</li>
  </ul>
  
  <p>Thanks again</p>