This is what i’m trying to achieve:
I need to do an Insert on a table in a different database (same engine), then insert “locally”, then for each inserted i have to do a loop to insert in a child table.
The scenario is this: Person, Contract, PayOrder (local), PayOrder (db2), Contracts_PayOrder. (The code itself isn’t in english, but I added comments)
For each contract that a person have, I need to create ONE PayOrder (in the 2 different databases) with a value that is the sum of all contracts, Then again, for each contract I have to make an insert on Contracts_PayOrder
I created 2 different .edmx for that. This is what i’m getting when trying to SaveChanges on the LocalDB: (Read the comments on the code)
System.Data.SqlClient.SqlException: New transaction is not allowed
because there are other threads running in the session.
This is my attempt:
using (RegistroContratoEntities dbs = new RegistroContratoEntities())
{
//Get list of active PERSONS
IQueryable<Credor> credorList = dbs.Credores.Where(c => c.Status == true);
foreach (Credor credor in credorList)
{
//Keep the payOrder Id
decimal? renavamBoletoId = null;
//Get list of contracts for that person
IQueryable<Contrato> contratosCredor = dbs.Contratos
.Where(c => c.Credor_Id == credor.Id &&
c.DataRegistro.Value.Month == pMesReferencia &&
c.Status == true);
if (contratosCredor.Count() > 0)
{
// Gets the total cost of the PayOrder
decimal valorBoleto = contratosCredor.Count() * 11;
//
// Creates the PayOrder (One for each person) on the external db
//
using (RenavamEntities dbren = new RenavamEntities())
{
BoletoRenavam boletoREN = new BoletoRenavam();
boletoREN.Arquivo = null;
boletoREN.BoletoSituacao_Id = CodBoletoSituacao.Ativo;
boletoREN.ConvenioBoleto_Id = CodBoletoConvenio.Padrao;
boletoREN.Emissao = DateTime.Now;
boletoREN.LogDataAlteracao = DateTime.Now;
boletoREN.LogUsuario = "RegistroContratos";
boletoREN.LogVersaoRegistro = 1;
boletoREN.Pagamento = null;
boletoREN.PagamentoValor = null;
boletoREN.Processamento = null;
boletoREN.Status = "S";
boletoREN.ParcelaValor = valorBoleto;
boletoREN.Vencimento = DateTime.Now.AddDays(15);
dbren.AddToBoleto(boletoREN);
dbren.SaveChanges();
renavamBoletoId = boletoREN.Id;
}
//
// The PayOrder on the "main"" db
//
Boleto boletoREG = new Boleto();
boletoREG.NumeroBoletoRenavam = renavamBoletoId;
boletoREG.ArquivoRetorno = null;
boletoREG.BoletoConvenio_Id = CodBoletoConvenio.Padrao;
boletoREG.BoletoSituacao_Id = CodBoletoSituacao.Ativo;
boletoREG.Credor_Id = credor.Id;
boletoREG.Emissao = DateTime.Now;
boletoREG.LogAlteracao = DateTime.Now;
boletoREG.LogUsuario = pUsuario;
boletoREG.LogVersao = 1;
boletoREG.NumeroBoletoRenavam = null;
boletoREG.Pagamento = null;
boletoREG.PagamentoValor = null;
boletoREG.Processamento = null;
boletoREG.Status = true;
boletoREG.Valor = valorBoleto;
boletoREG.Vencimento = DateTime.Now.AddDays(15);
dbs.AddToBoletos(boletoREG);
dbs.SaveChanges();
//
// Then, for each contract, inserts a row in a child table that
// will relate all contracts with it's PayOrders
foreach (Contrato contrato in contratosCredor)
{
BoletoTaxaContrato boletoContrato = new BoletoTaxaContrato();
boletoContrato.Boleto = boletoREG;
boletoContrato.Contrato = contrato;
boletoContrato.Data = DateTime.Now;
boletoContrato.LogAlteracao = DateTime.Now;
boletoContrato.LogUsuario = pUsuario;
boletoContrato.LogVersao = 1;
boletoContrato.Quantidade = 1;
boletoContrato.Taxa_Id = CodBoletoTaxa.RegistroContrato;
dbs.AddToBoletoTaxaContratos(boletoContrato);
dbs.SaveChanges();
}
}
}
}
Just close first context before opening new one.
I know that you need some data from another context but you can load it before (cast it to list|entity for example to execute your query before first context will be closed).