コード例 #1
0
        public void MigrateAndSeedData()
        {
            if (Database.GetPendingMigrations().Any())
            {
                Database.Migrate();
            }

            if (!Components.Any())
            {
                //Seed database here
                var imagePath = Path.Combine(Directory.GetCurrentDirectory(), @".\seedImages\pump.jpg");
                var esImage   = new EsImage
                {
                    ImageMimeType = "image/jpeg",
                    ImageData     = Util.Util.ImageToByteArray(imagePath, FREE_IMAGE_FORMAT.FIF_JPEG),
                    Thumbnail     = Util.Util.ThumbNailByteArray(imagePath, FREE_IMAGE_FORMAT.FIF_JPEG)
                };

                var imagePath2 = Path.Combine(Directory.GetCurrentDirectory(), @"..\seedImages\pump2.jpg");
                var esImage2   = new EsImage
                {
                    ImageMimeType = "image/jpeg",
                    ImageData     = Util.Util.ImageToByteArray(imagePath, FREE_IMAGE_FORMAT.FIF_JPEG),
                    Thumbnail     = Util.Util.ThumbNailByteArray(imagePath, FREE_IMAGE_FORMAT.FIF_JPEG)
                };

                var componentTypes = new[]
                {
                    new ComponentType
                    {
                        ComponentName = "Pumpe",
                        AdminComment  = "",
                        ComponentInfo = "",
                        Datasheet     = "Datasheet",
                        Status        = ComponentTypeStatus.Available,
                        Location      = "Kontoret",
                        Manufacturer  = "Skolen",
                        WikiLink      = "https://da.wikipedia.org/wiki/Pumpe",
                        ImageUrl      = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Drehkolbenpumpe.jpg/220px-Drehkolbenpumpe.jpg",
                        Image         = esImage
                    },
                    new ComponentType
                    {
                        ComponentName = "Pumpe2",
                        AdminComment  = "",
                        ComponentInfo = "",
                        Datasheet     = "Datasheet",
                        Status        = ComponentTypeStatus.Available,
                        Location      = "Kontoret",
                        Manufacturer  = "Skolen",
                        WikiLink      = "https://da.wikipedia.org/wiki/Pumpe",
                        ImageUrl      = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Drehkolbenpumpe.jpg/220px-Drehkolbenpumpe.jpg",
                        Image         = esImage2
                    }
                };

                ComponentTypes.AddRange(componentTypes);
                SaveChanges();


                var components = new Component[]
                {
                    new Component
                    {
                        AdminComment             = "Pumpe",
                        ComponentNumber          = 1,
                        SerialNo                 = "123456789",
                        Status                   = ComponentStatus.Available,
                        UserComment              = "Lånt i 1 dag",
                        CurrentLoanInformationId = null
                    },
                    new Component
                    {
                        AdminComment             = "Pumpe",
                        ComponentNumber          = 2,
                        SerialNo                 = "234567891",
                        Status                   = ComponentStatus.ReservedAdmin,
                        UserComment              = "Lånt i 2 dage",
                        CurrentLoanInformationId = null
                    },
                    new Component
                    {
                        AdminComment             = "Pumpe",
                        ComponentNumber          = 3,
                        SerialNo                 = "345678912",
                        Status                   = ComponentStatus.Available,
                        UserComment              = "Lånt i 3 dage",
                        CurrentLoanInformationId = null
                    },
                    new Component
                    {
                        AdminComment             = "Pumpe",
                        ComponentNumber          = 4,
                        SerialNo                 = "456789123",
                        Status                   = ComponentStatus.Defect,
                        UserComment              = "I stykker",
                        CurrentLoanInformationId = null
                    },
                    new Component
                    {
                        AdminComment             = "Pumpe",
                        ComponentNumber          = 5,
                        SerialNo                 = "567891234",
                        Status                   = ComponentStatus.Available,
                        UserComment              = "Lånt i 5 dage",
                        CurrentLoanInformationId = null
                    }
                };

                var currentComponentType = ComponentTypes.FirstOrDefault();
                foreach (var component in components)
                {
                    currentComponentType.Components.Add(component);
                }
                SaveChanges();

                var categories = new[]
                {
                    new Category
                    {
                        Name = "Motordrevet"
                    },
                    new Category
                    {
                        Name = "Elektronik"
                    }
                };

                Categories.AddRange(categories);
                SaveChanges();

                var componentTypeCategoies = new[]
                {
                    new ComponentTypeCategory
                    {
                        ComponentType = ComponentTypes.FirstOrDefault(),
                        Category      = Categories.FirstOrDefault()
                    }
                };

                ComponentTypeCategory.AddRange(componentTypeCategoies);
                SaveChanges();
            }
        }