barteloma
barteloma

Reputation: 6875

Should I close Entity Framework dbcontext connection after raw query?

I am using entity framework database raw query.

 using var command = context.Database.GetDbConnection().CreateCommand();
 command.CommandText = "select * from my_school";

 using var dataReader = command.ExecuteReader();
 var dataRow = ReadSchools(dataReader);

 command.Connection.Close();????

After dataReader read the result, should I close the connection or does using statement close the connection after scope? If I don't close, does the connection pool fill?

Upvotes: 0

Views: 107

Answers (1)

Selim Yildiz
Selim Yildiz

Reputation: 5370

You can use using for DBConnection as following:

using (var conn = context.Database.GetDbConnection())
{
 var command = conn.CreateCommand();
 command.CommandText = "select * from my_school";

 var dataReader = command.ExecuteReader();
 var dataRow = ReadSchools(dataReader);

 }

For more detail please check Working with DbContext

Upvotes: 1

Related Questions