简体   繁体   English

EntityFramework在长时间运行的任务上等待操作超时

[英]EntityFramework The wait operation timed out on long running task

I'm using EF5 to migrate some data from one database to another. 我正在使用EF5将一些数据从一个数据库迁移到另一个数据库。 I would generally use SQL for something like this however I need other functionality (like creating users in the MembershipProvider ) and was hoping to do it all in EF. 我通常会使用SQL来实现类似的功能,但是我需要其他功能(例如在MembershipProvider创建用户),并希望在EF中做到所有这些。 I'm migrating about 100k rows and using this to do so: 我正在迁移约10万行,并使用它来这样做:

        using (var connection = new SqlConnection(connectionString))
        { 
            using(var command = new SqlCommand(commandText, connection))
            {
                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var employer = new Employer();

                        employer.EAN = reader["EAN"].ToString();

                        employer.Name = GetString(reader["EmpName"]);

                        employer.TaxMailingAddress = new Address
                        {
                            StreetAddress = GetString(reader["Street"]),
                            City = GetString(reader["City"]),
                            State = GetString(reader["USState"]),
                            ZipCode = GetString(reader["Zip"])
                        };

                        employer.TaxMailingAddress.SaveOrUpdate();
                        employer.SaveOrUpdate(); // This is where the timeout happens

                        string dba = GetString(reader["DBA"]);
                        if (!string.IsNullOrWhiteSpace(dba))
                        {
                            employer.AddDBA(new EmployerDba 
                            {
                                Name = dba
                            });
                        }

                        string email = GetString(reader["Email"]);
                        if (!string.IsNullOrWhiteSpace(email))
                        {
                            var user = CreateNewUser(email);

                            if (user != null)
                            {
                                user.AddAuthorizedEmployer(employer);

                                user.AddRole(EmployerRole.Admin, employer, true);
                            }
                        }
                    }
                }
            }
        }

My SaveOrUpdate method is pretty straight forward: 我的SaveOrUpdate方法非常简单:

    public void SaveOrUpdate()
    {
        using (var db = new MyContext())
        {
            if (db.Employers.FirstOrDefault(x => x.EAN == EAN && x.Id != Id) != null)
                throw new Exception("An employer with this EAN has already been registered.");

            var employer = new Employer();

            if (Id == 0)
            {
                db.Employers.Add(employer);

                employer.CreatedBy = Statics.GetCurrentUserName();
                employer.DateCreated = DateTime.Now;
            }
            else
            {
                employer = db.Employers.FirstOrDefault(x => x.Id == Id);

                employer.ModifiedBy = Statics.GetCurrentUserName();
                employer.DateModified = DateTime.Now;
            }

            employer.EAN = EAN;
            employer.Name = Name;
            if (TaxMailingAddress != null) employer.TaxMailingAddress = db.Addresses.FirstOrDefault(x => x.Id == TaxMailingAddress.Id);
            if (SingleSeparationStatementAddress != null) employer.SingleSeparationStatementAddress = db.Addresses
                                    .FirstOrDefault(x => x.Id == SingleSeparationStatementAddress.Id);

            db.SaveChanges();

            Id = employer.Id;
        }
    }

The task should take about 2.5 hours to complete. 该任务大约需要2.5个小时才能完成。 However, after running many thousands of rows, sometime 80k, sometimes as few as 7k, I get this "The wait operation timed out" exception, always on the employer.SaveOrUpdate(); 但是,在运行了数千行(有时是80k,有时甚至是7k)之后,我得到了这个"The wait operation timed out"异常,该异常总是在employer.SaveOrUpdate(); . Could it have anything to do with how close it is to the employer.TaxMailingAddress.SaveOrUpdate(); 它与employer.TaxMailingAddress.SaveOrUpdate();有多近有关系吗? employer.TaxMailingAddress.SaveOrUpdate(); ? Is there a "wait for the transaction to complete" deal? 是否有“等待交易完成”交易? Perhaps make sure the connection is valid and if not try recreating it or something? 也许要确保连接有效,如果不尝试重新建立连接或其他方法? Thanks for any help. 谢谢你的帮助。

The problem ended up being that my initial database connection that was being used to connect to the other database, from which I was getting the data to import, was timing out. 问题最终导致我用于连接其他数据库的初始数据库连接超时,而我正从该数据库中导入数据。 So I ended up looping through all of the reader.Read() statements and putting that into an array. 因此,我最终遍历了所有reader.Read()语句,并将其放入数组中。 I then looped through that array to actually process and save the data into my new database. 然后,我遍历该数组以实际处理数据并将其保存到新数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM