2

我正在使用EF5将一些数据从一个数据库迁移到另一个数据库。我通常会使用SQL这样的东西,但是我需要其他功能(比如在 中创建用户MembershipProvider)并希望在 EF 中完成所有操作。我正在迁移大约 100k 行并使用它来执行此操作:

        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);
                            }
                        }
                    }
                }
            }
        }

我的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;
        }
    }

该任务大约需要 2.5 小时才能完成。但是,在运行了数千行之后,有时是 80k,有时是 7k,我得到了这个"The wait operation timed out"异常,总是在employer.SaveOrUpdate();. 它与它的距离有多近有关系employer.TaxMailingAddress.SaveOrUpdate();吗?是否有“等待交易完成”的交易?也许确保连接有效,如果没有尝试重新创建它或其他什么?谢谢你的帮助。

4

1 回答 1

3

问题最终是我用于连接到另一个数据库的初始数据库连接(我从中获取要导入的数据)超时。所以我最终遍历了所有reader.Read()语句并将其放入数组中。然后我遍历该数组以实际处理数据并将其保存到我的新数据库中。

于 2013-10-14T18:49:13.500 回答