I do not have access to the server where the application is hosted, and from time to time it undergoes some migrations to other servers, so I was directed to avoid using techniques that make it necessary to have any extra resources (which is not by default ).
I'm developing a feature in the application that needs a transaction, but by default the EF closes the connection on the SaveChanges()
call, so I get a exception
when trying to make any new requests through my Entities
object% I get the following exception:
The underlying provider failed in Open.
I then tried to instantiate my context
at different times for each change, but the same error persists.
using(TransactionScope ts = new System.Transactions.TransactionScope())
{
var teste1 = new Teste();
using (var context = new MyEntities())
{
teste1.Nome = "Teste1";
context.Add(teste1);
context.SaveChanges();
}
// Até aqui tudo está funcionando bem!
using (var context = new MyEntities())
{
//Erro nessa linha!
var usuario = context.User.SingleOrDefault(u => u.UserId == ViewSessionContext.UserId);
if(usuario != null)
{
var teste2 = new FilhoTeste();
teste2.Nome = "FilhoTeste";
teste2.TesteId = teste1.Id;
teste2.UserId = usuario.Id;
teste2.Add(teste2);
context.SaveChanges();
}
}
ts.Complete();
}
Would it be possible to use a transaction without having to enable DTC ?
I also tried to force the connection to open, but I received the same error!
Search sources: