title: Initialize data with Entity Framework
body: <p>I want to initialize dummy data to the database before the application runs. I have a SQL script which inserts data directly to the tables.</p>
  
  <p>The problem is when I want to update an element I get this exception:</p>
  
  <p><code>'System.InvalidOperationException' en EntityFramework.dll: Attaching an entity of type 'Es.Udc.DotNet.MiniPortal.Model.Book' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.</code></p>
  
  <p>This is my update method:</p>
  
  <pre><code>public void Update(E entity)
  {
      dbContext.Entry&lt;E&gt;(entity).State = EntityState.Modified;
      dbContext.SaveChanges();
  }
  </code></pre>
  
  <p>I think that's happening because EF doesn't track the items, but I don't know what can I do. What's the best approach to initialize the dummy data and get tracked by EF? Thanks.</p>