Entity Framework – Tips and Tricks

How do I stop my Code First implementation creating a new database

It might seem a strange request, why use Code First then. But let’s assume you’ve created a Code First EF model and then switched to creating the db using SQL scripts and want ensure the EF application doesn’t try to create the db.

Database.SetInitializer(null);

By default the initializer would be set to CreateDatabaseIfNotExists.

See also Database.SetInitializer.

There is already an open DataReader associated with this Command which must be closed first

An EntityCommandExecutionException with the message “There is already an open DataReader associated with this Command which must be closed first” occurred whilst implementing a bit of code to list all the ProductTypes in a database.

The solution appears to be add

MultipleActiveResultSets=True

to your connection string

I’ll add more to this post as or when I remember