Ejemplo n.º 1
0
        public static void Install(IServiceProvider provider)
        {
            var unitOfWork     = provider.GetService <IUnitOfWork <EfDbContext> >();
            var repositoryUser = provider.GetService <IRepository <User> >();
            var user           = repositoryUser.Get(x => x.Username == "atif.dag");

            var listParameterGroup        = new List <ParameterGroup>();
            var listParameterGroupHistory = new List <ParameterGroupHistory>();
            var listParameter             = new List <Parameter>();
            var listParameterHistory      = new List <ParameterHistory>();

            var counterParameterGroup = 1;

            foreach (var(key, value) in ParameterGroups)
            {
                var item = new ParameterGroup
                {
                    Id                   = GuidHelper.NewGuid(),
                    CreationTime         = DateTime.Now,
                    LastModificationTime = DateTime.Now,
                    DisplayOrder         = counterParameterGroup,
                    Version              = 1,
                    IsApproved           = true,
                    Code                 = key,
                    Name                 = value,
                    Description          = value,
                    Creator              = user,
                    LastModifier         = user
                };

                var itemHistory = item.CreateMapped <ParameterGroup, ParameterGroupHistory>();
                itemHistory.Id             = GuidHelper.NewGuid();
                itemHistory.ReferenceId    = item.Id;
                itemHistory.CreatorId      = item.Creator.Id;
                itemHistory.IsDeleted      = false;
                itemHistory.RestoreVersion = 0;
                counterParameterGroup++;
                listParameterGroup.Add(item);
                listParameterGroupHistory.Add(itemHistory);
            }

            unitOfWork.Context.AddRange(listParameterGroup);
            unitOfWork.Context.AddRange(listParameterGroupHistory);
            unitOfWork.Context.SaveChanges();

            var counterParameter   = 1;
            var listParameterCount = Parameters.Count;

            foreach (var(item1, item2, item3) in Parameters)
            {
                var itemParameterGroup = unitOfWork.Context.Set <ParameterGroup>().FirstOrDefault(x => x.Code == item1);

                var item = new Parameter
                {
                    Id                   = GuidHelper.NewGuid(),
                    Key                  = item2,
                    Value                = item3,
                    CreationTime         = DateTime.Now,
                    LastModificationTime = DateTime.Now,
                    DisplayOrder         = counterParameter,
                    Version              = 1,
                    IsApproved           = true,
                    Creator              = user,
                    LastModifier         = user,
                    ParameterGroup       = itemParameterGroup
                };

                listParameter.Add(item);

                var itemHistory = item.CreateMapped <Parameter, ParameterHistory>();
                itemHistory.Id               = GuidHelper.NewGuid();
                itemHistory.ReferenceId      = item.Id;
                itemHistory.ParameterGroupId = item.ParameterGroup.Id;
                itemHistory.CreatorId        = item.Creator.Id;
                itemHistory.IsDeleted        = false;
                itemHistory.RestoreVersion   = 0;

                listParameterHistory.Add(itemHistory);

                Console.WriteLine(counterParameter + @"/" + listParameterCount + @" Parameter (" + item.Key + @")");

                counterParameter++;
            }

            unitOfWork.Context.AddRange(listParameter);
            unitOfWork.Context.AddRange(listParameterHistory);
            unitOfWork.Context.SaveChanges();
        }