ho davvero provato questa cosa sia ADO.net e EF v.6 e guardato le connessioni nella tabella SQL
select * from sys.dm_exec_connections
metodi da testare si presentava così:
1) ADO.net con utilizzando
using(var Connection = new SqlConnection(conString))
{
using (var command = new SqlCommand(queryString, Connection))
{
Connection.Open();
command.ExecuteNonQueryReader();
throw new Exception() // Connections were closed after unit-test had been
//finished. Expected behaviour
}
}
2) ADO.net withour utilizzando
var Connection = new SqlConnection(conString);
using (var command = new SqlCommand(queryString, Connection))
{
Connection.Open();
command.ExecuteNonQueryReader();
throw new Exception() // Connections were NOT closed after unit-test had been finished
finished. I closed them manually via SQL. Expected behaviour
}
1) EF con utilizzo.
using (var ctx = new TestDBContext())
{
ctx.Items.Add(item);
ctx.SaveChanges();
throw new Exception() // Connections were closed, as expected.
}
2) EF senza usare
var ctx = new TestDBContext();
ctx.Items.Add(item);
ctx.SaveChanges();
throw new Exception() // Connections WERE successfully closed, as NOT expected.
Non so perché è così, ma EF connessioni automaticamente chiuso. Inoltre, tutti i modelli di repository e UnitOfWork che utilizzano EF non utilizzano l'utilizzo. È molto strano per me, perché DBContext è di tipo Usa e getta, ma è un dato di fatto.
Forse in Microsoft hanno fatto qualcosa di nuovo per la gestione?
fonte
2013-11-07 21:20:40
Vero, ma la domanda è ciò che il metodo Dispose() relly fa per il contesto EF . Sembra che non sia veramente importante da quello che posso trovare sull'argomento! –
"Responsible" ... Non è solo per codice non gestito? – ullmark
Grazie per aver chiesto questa domanda. Come risultato, ho scoperto che tutte le chiamate di Dispose che temo di dimenticare nella nostra applicazione potrebbero non essere così critiche come temevo (leggendo http://lee.hdgreetings.com/2008/06/linq-datacontex. html). – BlueMonkMN