How do I detach objects in Entity Framework Code First?

Solution 1:

This is an option:

dbContext.Entry(entity).State = EntityState.Detached;

Solution 2:

If you want to detach existing object follow @Slauma's advice. If you want to load objects without tracking changes use:

var data = context.MyEntities.AsNoTracking().Where(...).ToList();

As mentioned in comment this will not completely detach entities. They are still attached and lazy loading works but entities are not tracked. This should be used for example if you want to load entity only to read data and you don't plan to modify them.