コード例 #1
0
        public void ProcessRaceReport(RaceReport raceReport)
        {
            var currentReportsRace = _raceService.GetRace(raceReport.Race.Id.Value);

            _context.Races.Attach(currentReportsRace);
            raceReport.Race = currentReportsRace;

            CheckForPersonalBests(raceReport);
            ProcessIndividualResults(raceReport);

            var datestring = currentReportsRace.Date.ToShortDateString();

            var path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/content/excelfiles/"),
                                    currentReportsRace.RaceName + datestring.Replace('/', '-') + ".xlsx");

            ExcelDoc ex = new ExcelDoc();

            ex.CreateNewFile();

            WriteNameColumn(ex, raceReport);
            WriteTimeColumn(ex, raceReport);
            WritePlaceColumn(ex, raceReport);

            ex.SaveAs(path);
            ex.Close();

            FileStream       fs     = new FileStream(path, FileMode.Open, FileAccess.Read);
            BinaryReader     reader = new BinaryReader(fs);
            RaceReportBinary binary = new RaceReportBinary
            {
                RaceId = raceReport.Race.Id,

                Data     = reader.ReadBytes((Int32)fs.Length),
                FileName = currentReportsRace.RaceName + datestring.Replace('/', '-') + ".xlsx"
            };

            var reportindb = _context.RaceReports.SingleOrDefault(r => r.FileName == binary.FileName);

            if (reportindb == null)
            {
                _context.RaceReports.Add(binary);
            }
            else
            {
                reportindb.FileName = binary.FileName;
                reportindb.Data     = binary.Data;
            }

            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                       eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                                           ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }

            fs.Close();
            reader.Close();

            System.IO.File.Delete(path);
        }