簡體   English   中英

LINQ to具有多個數據庫的實體

[英]LINQ to Entities with Multiple databases

這就是我想要實現的目標:

我需要在不同數據庫(同一引擎)中的表上執行插入操作,然后“本地”插入,然后對於每個插入的操作,我都必須做一個循環才能插入子表中。

該方案是這樣的: PersonContractPayOrder (本地), PayOrder (db2), Contracts_PayOrder (代碼本身不是英語,但我添加了注釋)

對於一個person擁有的每個contract ,我需要創建一個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();
            }
        }
    }
}

在打開新上下文之前,只需關閉第一個上下文。

我知道您需要來自其他上下文的一些數據,但是您可以在加載之前將其加載(例如,將其廣播到list | entity以在關閉第一個上下文之前執行查詢)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM