We have a table with periods, and other child tables with these periods and values for combinations of product + location.
Based on selected periods, I would like to add any missing rows in the child tables. I have come up with the following code, which seems to work just fine. But I would like to know if there is a simpler or more efficient way of doing this, since I have to repeat this for a number of tables.
public static void SeedActivityData(int startPeriod, double endPeriod, IList<DAL.OMS_StockStatus> stockStatus)
{
DAL.EFDbContext dbFilter = new DAL.EFDbContext(0, stockStatus.FirstOrDefault().Product, startPeriod, endPeriod);
var period = dbFilter.Periods.Select(b => b.PeriodID);
foreach (var item in stockStatus)
{
IEnumerable<int> missingFAF = period.Except(dbFilter.OMS_Forecast_Adjustment.Where(a => a.SiteID == item.SiteID).Select(b => b.PeriodID));
var modelFAF = db.OMS_Forecast_Adjustment;
foreach (int periodID in missingFAF)
{
var modelItem = new OMS_Forecast_Adjustment { PeriodID = periodID, Product = item.Product, SiteID = item.SiteID, Value = 0 };
modelFAF.Add(modelItem);
}
IEnumerable<int> missingRAF = period.Except(dbFilter.OMS_Receipts_Adjustment.Where(a => a.SiteID == item.SiteID).Select(b => b.PeriodID));
var modelRAF = db.OMS_Receipts_Adjustment;
foreach (int periodID in missingRAF)
{
var modelItem = new OMS_Receipts_Adjustment { PeriodID = periodID, Product = item.Product, SiteID = item.SiteID, Value = 0 };
modelRAF.Add(modelItem);
}
db.SaveChanges();
}
}