dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.62k stars 1.96k forks source link

Document other "raw" database access APIs besides FromSql #665

Open divega opened 6 years ago

divega commented 6 years ago

We should add more information about ExecuteSqlCommand and GetDbConnection, including how to manage the open/close state (e.g. with context.Database.OpenConnection and context.Database.CloseConnection vs. connection.Open and connection.Close) and lifetime of the connection returned with the latter (see https://github.com/aspnet/EntityFrameworkCore/issues/11415).

Mart-Bogdan commented 6 years ago

@divega can you briefly describe here should user close connections obtained by GetDbConnection, or it would be auto-closed with Context?

ajcvickers commented 6 years ago

@Mart-Bogdan For DbConnections in general with EF Core:

GetDbConnection gives access to the underlying DbConnection that EF is using, so you shouldn't dispose it unless you originally created the connection and passed it to EF in the first place. With regard to closing, it's best practice if you open it to then close it.

ajcvickers commented 4 years ago

See also #1787

roji commented 2 years ago

Splitting #1787 out again, since this issue seems to be (also) about DbConnection lifecycle/ownership issues which aren't unique to ExecuteSql (e.g. also ExecuteUpdate, even querying?). Let's discuss...