title: EF code first - not able to update a database row
body: <p>Was trying out some code with the EF "code first" method and ran into a strange problem. </p>
  
  <p>My datacontext:</p>
  
  <pre><code>    public class BookmarkerDataContext : DbContext
      {
          public DbSet&lt;User&gt; Users { get; set; }
          protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;User&gt;().HasKey(u =&gt; u.UserId);
              base.OnModelCreating(modelBuilder);
          }
       }
  </code></pre>
  
  <p>Where the user object is:</p>
  
  <pre><code>   public class User
      {
          public long UserId { get; set; }
          public ICollection&lt;Tag&gt; Tags { get; set; }
      }
  </code></pre>
  
  <p>In my code I am doing something fairly simple:</p>
  
  <pre><code>public void UpdateUserTags(User user,ICollection&lt;Tag&gt; taglist)
      {
          user.Tags = new List&lt;Tag&gt;(user.Tags.Union(taglist));
          datacontext.Users.Add(user);
          datacontext.SaveChanges();
      }
  </code></pre>
  
  <p>The user object I am passing to this function is the result of something like:</p>
  
  <pre><code>datacontext.Users.SingleOrDefault(u =&gt; u.UserId==id)
  </code></pre>
  
  <p>Everytime I call the UpdateUserTags function it seems to create a new Row in the User table instead of updating it. Am I doing something wrong here?  </p>