Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:15:29+00:00 2026-06-03T22:15:29+00:00

This is what i’m trying to achieve: I need to do an Insert on

  • 0

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();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:15:31+00:00Added an answer on June 3, 2026 at 10:15 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my database table. Now I want to combine Branch_Id, Acc_no and Acc_Type
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This isn't about the different methods I could or should be using to utilize
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This is really just for my own use: I would like to be able
This somehow simple task is not so simple. I can get the number of
This has been a rather problematic issue on numerous occasions. We have alot of
This should be simple, but the answer is eluding me. If I've got a
this is my code that I write it but I want to use LINQ

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.