title: EntityFramework 5 Code First - lookup table gets duplicate records
body: <p>I am trying the EF5 CodeFirst and cannot get the simple setup to work ;(</p>
  
  <p>I have two classes Foo and Bar where Bar represent lookup table.</p>
  
  <pre><code>public class Foo
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual Bar Bar { get; set; }
  
  }
  
  public class Bar
  {
      public int Id { get; set; }
      public string Description { get; set; }
  }
  
  public class MyDbContext : DbContext
  {
      static MyDbContext()
      {
          Database.SetInitializer&lt;MyDbContext&gt;(null);
      }
  
      public MyDbContext(): base("testEF"){}
  
      public DbSet&lt;Foo&gt; Foos { get; set; }
      public DbSet&lt;Bar&gt; Bars { get; set; }
  }
  </code></pre>
  
  <p>Now I have created a static class that serves as DataAccess Layer - in real-world application it will be on different physical tier</p>
  
  <pre><code>public static class DataAccess
  {
      public static Bar GetBarById(int id)
      {
          using (var db = new MyDbContext())
          {
              return db.Bars.SingleOrDefault(b =&gt; b.Id == id);
          }
      }
  
      public static Foo InsertFoo(Foo foo)
      {
          using (var db = new MyDbContext())
          {
              db.Foos.Add(foo);
  
              db.SaveChanges();
          }
          return foo;
      }
  }
  </code></pre>
  
  <p>I am initializing the DB with seed method:</p>
  
  <pre><code>internal sealed class Configuration : DbMigrationsConfiguration&lt;testEF.MyDbContext&gt;
  {
      public Configuration()
      {
          AutomaticMigrationsEnabled = false;
      }
      protected override void Seed(testEF.MyDbContext context)
      {
              context.Bars.AddOrUpdate(
                  new Bar { Description = "Bar_1" },
                  new Bar { Description = "Bar_2" }
  
                  );
      }
  }
  </code></pre>
  
  <p>This creates two records in Bars table. So far so good... </p>
  
  <p>Here is my Main function</p>
  
  <pre><code>static void Main(string[] args)
  {
      var bar1 = DataAccess.GetBarById(1); 
  
      var foo = new Foo
      {
          Name = "Foo_1",
          Bar = bar1
      };
  
      DataAccess.InsertFoo(foo);
  
  }
  </code></pre>
  
  <p>After the app runes there is a record in the Foos table:</p>
  
  <pre><code>Id       Name    Bar_Id
  1        Foo_1   3   
  </code></pre>
  
  <p>Why Bar_Id is 3? The EF actually inserted new record to Bars table!</p>
  
  <pre><code>Id  Description
  1   Bar_1
  2   Bar_2
  3   Bar_1
  </code></pre>
  
  <p>What I am doing wrong?</p>
  
  <p><strong>UPDATE:</strong>
  I have found a workaround - to attach Bar property prior to inserting the record:</p>
  
  <pre><code>public static Foo InsertFoo(Foo foo)
  {
      using (var db = new MyDbContext())
      {
          db.Bars.Attach(foo.Bar);
  
          db.Foos.Add(foo);
  
          db.SaveChanges();
      }
      return foo;
  }
  </code></pre>
  
  <p>It is working now but this is more like a hack than a valid solution...
  In real-world application the complexity of the objects could become a huge problem. 
  I am open to better solutions</p>