我正在方法Task.Run(() => AircraftManager.UploadAircraft(fileContent));中的BulkInsert()中调用一个方法,但未在数据库中添加记录。但是,如果我在没有Task.Run()的情况下运行,则效果很好,但是需要更长的时间。

注意:Uploading Aircraft-2019.csv包含超过22,000条记录。

这是我的代码

Controller.cs

[HttpPost]
        public IHttpActionResult UploadAircraft()
        {
            //If the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var provider = currentRequest.Value.Content.ReadAsMultipartAsync(new Classes.Mach.AuthorizationServer.API.Providers.MultipartFormDataStreamProvider()).Result;
            IList<HttpContent> receivedfiles = provider.Files;

            //Upload multiple files
            List<FileUploadResult> uploadResults = new List<FileUploadResult>();
            foreach (HttpContent file in receivedfiles)
            {
                var receivedFileName = file.Headers.ContentDisposition.FileName.Trim('\"');
                var receivedkey = file.Headers.ContentDisposition.Name.Trim('\"');

                byte[] fileContent = file.ReadAsByteArrayAsync().Result;

                Task.Run(() => AircraftManager.UploadAircraft(receivedkey, receivedFileName, fileContent));

            }
            return Ok(new ApiResponse(true, "Files uploaded successfully", uploadResults));
        }


AircraftManager.cs

public void UploadAircraft(string documentType, string filename, byte[] content)
{
   if (filename.ToLower().EndsWith("csv"))
    {
        using (MemoryStream stream = new MemoryStream(content))
        {
            using (CsvReader csvReader = new CsvReader(new StreamReader(stream), true))
            {
                var records = csvReader.GetRecords<AircraftDTO>();
                List<Aircraft> aircraftList = new List<Aircraft>();
                foreach (var item in records )
                {
                    Aircraft aircraft = new Aircraft();

                    aircraft.BusinessName = item.BusinessName;
                    aircraft.IssuingCountry = item.IssuingCountry;
                    aircraft.CertificateCode = item.CertificateCode;
                    aircraft.CertificateHolderName = item.CertificateHolderName;
                    aircraft.Tailnumber = item.Tailnumber;
                    aircraft.SerialNumber = item.SerialNumber;
                    aircraft.YearMade = !string.IsNullOrWhiteSpace(item.YearMade) && item.YearMade != "NULL" ? Convert.ToInt32(item.YearMade) : (int?)null;
                    aircraft.Manufacturer = item.Manufacturer

                    aircraftList.Add(wyvernAircraft);
                }
               dbContext.Aircraft.BulkInsert(aircraftList);
            }
        }
    }
}

最佳答案

只需使用从MemoryStream中删除​​dbContext调用,然后将dbContext.Aircraft.BulkInsert(aircraftList)替换为dbContext.BulkInsert(aircraftList),如下所示:


    List<Aircraft> aircraftList = new List<Aircraft>();
        using (MemoryStream stream = new MemoryStream(content))
        {
                using (CsvReader csvReader = new CsvReader(new StreamReader(stream), true))
                {
                    var records = csvReader.GetRecords<AircraftDTO>();

                    foreach (var item in records )
                    {
                        Aircraft aircraft = new Aircraft();

                        aircraft.BusinessName = item.BusinessName;
                        aircraft.IssuingCountry = item.IssuingCountry;
                        aircraft.CertificateCode = item.CertificateCode;
                        aircraft.CertificateHolderName = item.CertificateHolderName;
                        aircraft.Tailnumber = item.Tailnumber;
                        aircraft.SerialNumber = item.SerialNumber;
                        aircraft.YearMade = !string.IsNullOrWhiteSpace(item.YearMade) && item.YearMade != "NULL" ? Convert.ToInt32(item.YearMade) : (int?)null;
                        aircraft.Manufacturer = item.Manufacturer

                        aircraftList.Add(wyvernAircraft);
                    }
                }
        }
        dbContext.BulkInsert(aircraftList);

关于c# - EF BulkInsert未在Task.Run()中触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58062167/

10-12 19:55