具有多个数据库的LINQ to Entities

时间:2012-05-11 13:41:37

标签: c# asp.net linq entity-framework-4 linq-to-entities

这就是我想要实现的目标:

我需要在不同数据库(同一引擎)的表上执行Insert,然后插入“local”,然后对于每个插入的我必须执行循环以插入子表。

方案如下:PersonContractPayOrder(本地),PayOrder(db2),Contracts_PayOrder。 (代码本身不是英文,但我添加了评论)

对于contract拥有的每个person,我需要创建一个PayOrder(在2个不同的数据库中),其值为所有合同的总和,然后,对于每个合同,我必须在Contracts_PayOrder

上插入

我为此创建了2个不同的.edmx。这是我在LocalDB上尝试SaveChanges时得到的:(阅读代码注释)

  

System.Data.SqlClient.SqlException:不允许新事务   因为会话中还有其他线程在运行。

这是我的尝试:

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 个答案:

答案 0 :(得分:0)

在打开新的上下文之前,先关闭第一个上下文。

我知道您需要来自另一个上下文的一些数据,但您可以在之前加载它(例如将其转换为list | entity以在第一个上下文关闭之前执行您的查询)。

相关问题