views:

22

answers:

0

I'm trying to perform an import from Excel into an EF container, but every time I attempt this, every new record gets inserted twice. The 'AddToEntitySet' call only ever happens once, as does the SaveChanges, but immediately afterward, there are two of each record. What am I doing wrong?

            int rowNum = 1;
            foreach (DataRow row in planTable.Rows)
            {
                // Import creates a new production record for each day not already in DB, or updates records already in DB.
                DailyProductionRecord dailyProductionRecord = null;
                int recs =
                    prodRecs.Where(
                        x =>
                        (x.RecordDate >= thisPeriodStart && x.RecordDate < nextPeriodStart) && x.PerformanceCategory.Department.DeptId == deptId).
                        Count();
                if (recs == 0)
                {
                    dailyProductionRecord = new DailyProductionRecord();
                    dailyProductionRecord.PerformanceCategory = cat;
                    //dailyProductionRecord.DisplayFormat = cat.DisplayFormat.FormatString;
                    //dailyProductionRecord.PerformanceCategoryReference.EntityKey = new EntityKey("ProductionEntities.PerformanceCategorySet", "CategoryId", cat.CategoryId);
                    int day = (int)(double)row[2];
                    DateTime productionDate = new DateTime(periodYear, periodMonth, day);
                    dailyProductionRecord.RecordDate = productionDate;
                    //productionEntities.AddToDailyProductionRecordSet(dailyProductionRecord);
                }

                dailyProductionRecord.PlanValue = pv;
                rowNum++;
            }
            productionEntities.SaveChanges(true);