小编典典

Entity Framework中的SqlException-不允许新事务,因为会话中正在运行其他线程

c#

我目前收到此错误:

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

在运行此代码时:

public class ProductManager : IProductManager
{
    #region Declare Models
    private RivWorks.Model.Negotiation.RIV_Entities _dbRiv = RivWorks.Model.Stores.RivEntities(AppSettings.RivWorkEntities_connString);
    private RivWorks.Model.NegotiationAutos.RivFeedsEntities _dbFeed = RivWorks.Model.Stores.FeedEntities(AppSettings.FeedAutosEntities_connString);
    #endregion

    public IProduct GetProductById(Guid productId)
    {
        // Do a quick sync of the feeds...
        SyncFeeds();
        ...
        // get a product...
        ...
        return product;
    }

    private void SyncFeeds()
    {
        bool found = false;
        string feedSource = "AUTO";
        switch (feedSource) // companyFeedDetail.FeedSourceTable.ToUpper())
        {
            case "AUTO":
                var clientList = from a in _dbFeed.Client.Include("Auto") select a;
                foreach (RivWorks.Model.NegotiationAutos.Client client in clientList)
                {
                    var companyFeedDetailList = from a in _dbRiv.AutoNegotiationDetails where a.ClientID == client.ClientID select a;
                    foreach (RivWorks.Model.Negotiation.AutoNegotiationDetails companyFeedDetail in companyFeedDetailList)
                    {
                        if (companyFeedDetail.FeedSourceTable.ToUpper() == "AUTO")
                        {
                            var company = (from a in _dbRiv.Company.Include("Product") where a.CompanyId == companyFeedDetail.CompanyId select a).First();
                            foreach (RivWorks.Model.NegotiationAutos.Auto sourceProduct in client.Auto)
                            {
                                foreach (RivWorks.Model.Negotiation.Product targetProduct in company.Product)
                                {
                                    if (targetProduct.alternateProductID == sourceProduct.AutoID)
                                    {
                                        found = true;
                                        break;
                                    }
                                }
                                if (!found)
                                {
                                    var newProduct = new RivWorks.Model.Negotiation.Product();
                                    newProduct.alternateProductID = sourceProduct.AutoID;
                                    newProduct.isFromFeed = true;
                                    newProduct.isDeleted = false;
                                    newProduct.SKU = sourceProduct.StockNumber;
                                    company.Product.Add(newProduct);
                                }
                            }
                            _dbRiv.SaveChanges();  // ### THIS BREAKS ### //
                        }
                    }
                }
                break;
        }
    }
}

模型#1-该模型位于开发服务器上的数据库中。 型号1
http://content.screencast.com/users/Keith.Barrows/folders/Jing/media/bdb2b000-6e60-4af0-a7a1-2bb6b05d8bc1/Model1.png

模型2-该模型位于Prod Server上的数据库中,并且每天通过自动Feed进行更新。
替代文字http://content.screencast.com/users/Keith.Barrows/folders/Jing/media/4260259f-bce6-43d5-9d2a-017bd9a980d4/Model2.png

注意-模型#1中带红色圆圈的项目是我用来“映射”到模型#2的字段。请忽略模型2中的红色圆圈:这是我遇到的另一个问题,现在可以回答。

注意:我仍然需要进行isDeleted支票,以便在DB1超出我们客户的库存范围时可以将其从DB1中软删除。

我要做的就是用这个特定的代码将DB1中的一家公司与DB2中的一个客户连接起来,从DB2中获取他们的产品列表,如果还没有,请在DB1中插入它。第一次应该是充分利用库存。每次在此运行时,除非夜间通入饲料中的新库存,否则什么都不会发生。

因此,最大的问题-如何解决我遇到的交易错误? 每次循环时都需要删除并重新创建上下文(对我来说没有意义)吗?


阅读 407

收藏
2020-05-19

共1个答案

小编典典

拔掉头发后,我发现发foreachloop是罪魁祸首。需要发生的是调用EF,但将其返回到IList<T>该目标类型的,然后在上循环IList<T>

例:

IList<Client> clientList = from a in _dbFeed.Client.Include("Auto") select a;
foreach (RivWorks.Model.NegotiationAutos.Client client in clientList)
{
   var companyFeedDetailList = from a in _dbRiv.AutoNegotiationDetails where a.ClientID == client.ClientID select a;
    // ...
}
2020-05-19