public static void LinkCategory()
 {
     using (var DWdb = new DWModel())
     {
         using (var db = new SSRS())
         {
             foreach (var timeoption in DWdb.Timing_Option.ToList())
             {
                 var prodTD = db.ProdTBs.Where(r => r.Code == timeoption.Fixture.Code &&
                                               r.WorkCenter == timeoption.Timing_WorkStations.Name).FirstOrDefault();
                 foreach (var item in prodTD.OptionTBs)
                 {
                     foreach (var param in item.ParametersTBs)
                     {
                         var cat = DWdb.Categories.Where(r => r.Name == param.ParamName).FirstOrDefault();
                         if (cat != null)
                         {
                             Console.WriteLine(cat.Name);
                         }
                     }
                 }
             }
         }
     }
 }
        public static void addWorkstations()
        {
            using (var db = new SSRS())
            {
                //var wks = db.ProdTBs.ToList();
                var wks = db.ProdTBs.Select(x => new { x.WorkCenter, x.OpCode }).Distinct().ToList();


                foreach (var item in wks)
                {
                    if (item.OpCode != null)
                    {
                        var wk = new Timing_WorkStations()
                        {
                            Name = item.WorkCenter,

                            OpCode = item.OpCode
                        };

                        using (var DWdb = new DWModel())
                        {
                            DWdb.Timing_WorkStations.Add(wk);
                            DWdb.SaveChanges();
                        }
                    }
                }
            }
        }
        public static void LinkParams()
        {
            using (var DWdb = new DWModel())
            {
                using (var db = new SSRS())
                {
                    foreach (var timeoption in DWdb.Timing_Option.ToList())
                    {
                        var prodTD = db.ProdTBs.Where(r => r.Code == timeoption.Fixture.Code &&
                                                      r.WorkCenter == timeoption.Timing_WorkStations.Name).FirstOrDefault();
                        foreach (var item in prodTD.OptionTBs)
                        {
                            foreach (var param in item.ParametersTBs)
                            {
                                var fixture  = timeoption.Fixture;
                                var category = fixture.CategoryAtFixtures.Where(r => r.Category.Name == param.ParamName).FirstOrDefault();
                                if (category != null)
                                {
                                    var parameter = category.ParameterAtCategoryAtFixtures.Where(r => r.Parameter.Code == param.ParamValue).FirstOrDefault();

                                    if (parameter != null)
                                    {
                                        //Console.WriteLine("OK");
                                        timeoption.Parameters.Add(parameter.Parameter);
                                        //DWdb.SaveChanges();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        public static void addTimeOption()
        {
            HashSet <Timing_Option> globalopts = new HashSet <Timing_Option>();

            List <Fixture> fixtures = new List <Fixture>();

            using (var DWdb = new DWModel())
            {
                using (var db = new SSRS())
                {
                    var prod = db.ProdTBs.ToList();
                    foreach (var item in prod)
                    {
                        foreach (var opt in item.OptionTBs)
                        {
                            var fixture     = DWdb.Fixtures.Where(r => r.Code == item.Code).FirstOrDefault();
                            var workstation = DWdb.Timing_WorkStations.Where(r => r.Name == item.WorkCenter).FirstOrDefault();
                            if (fixture != null && workstation != null)
                            {
                                var optnew = new Timing_Option()
                                {
                                    Name    = opt.OptionName,
                                    Time    = (decimal)opt.ProdTime,
                                    Fixture = fixture,

                                    Timing_WorkStations = workstation
                                };
                                //DWdb.Timing_Option.Add(optnew);
                                //DWdb.SaveChanges();
                                globalopts.Add(optnew);
                            }
                        }
                    }
                }

                foreach (var item in globalopts)
                {
                    DWdb.Timing_Option.Add(item);
                    DWdb.SaveChanges();
                }
            }
        }