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.
The problem is when I want to update an element I get this exception:
'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.
This is my update method:
public void Update(E entity)
{
dbContext.Entry<E>(entity).State = EntityState.Modified;
dbContext.SaveChanges();
}
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.