0

I am using EF and seeding our DB with some product data. The data with which I'm seeding has a part which will be repeated about 100 times. Rather than copy and paste my code I would rather populate my list with a method but as I am a newbie, I cannot seem to make this work properly:

Here is the code in context:

context.Products.AddOrUpdate(
            pr => pr.Name,
            new Product
            {
                Name = "3.5x5",
                ProductCategoryId = 1,
                ProductSubCategoryId1 = 1,
                ProductSubCategoryId2 = 3,
                VendorId = 1,
                HeightUnitId = 2,
                Height = (decimal)3.5,
                Width = 5,
                ProductOptions =
                new List<ProductOption>
                {
                    new ProductOption { Name = "Paper", InputTypeSingleOptionId = 1, InputTypeMultipleOptionId = 2, SortOrder = 1,
                        ProductOptionsDetails =
                        new List<ProductOptionsDetail>
                        {
                            new ProductOptionsDetail { Name = "Glossy", Value = "Glossy", IsDefault = true, SortOrder = 1 },
                            new ProductOptionsDetail { Name = "Matte", Value = "Matte", IsDefault = false, SortOrder = 2 },
                            new ProductOptionsDetail { Name = "Metallic", Value = "Metallic", IsDefault = false, SortOrder = 3 },
                            new ProductOptionsDetail { Name = "Lustre", Value = "Lustre", IsDefault = false, SortOrder = 4 }
                        }
                    },
                    new ProductOption { Name = "Color", InputTypeSingleOptionId = 1, InputTypeMultipleOptionId = 2, SortOrder = 2,
                        ProductOptionsDetails =
                        new List<ProductOptionsDetail>
                        {
                            new ProductOptionsDetail { Name = "Color", Value = "Color", IsDefault = true, SortOrder = 1 },
                            new ProductOptionsDetail { Name = "Black and white", Value = "Black and White", IsDefault = false, SortOrder = 2 },
                            new ProductOptionsDetail { Name = "Sepia", Value = "Sepia", IsDefault = false, SortOrder = 3 }
                        } 
                    },
                    new ProductOption { Name = "Texture", InputTypeSingleOptionId = 1, InputTypeMultipleOptionId = 2, SortOrder = 3,
                        ProductOptionsDetails =
                        new List<ProductOptionsDetail>
                        {
                            new ProductOptionsDetail { Name = "None", Value = "None", IsDefault = true, SortOrder = 1 },
                            new ProductOptionsDetail { Name = "Linen texture", Value = "Linen", IsDefault = false, SortOrder = 2 },                         
                            new ProductOptionsDetail { Name = "Canvas texture", Value = "Canvas", IsDefault = false, SortOrder = 3 },
                            new ProductOptionsDetail { Name = "Watercolor texture", Value = "Canvas", IsDefault = false, SortOrder = 4 },
                            new ProductOptionsDetail { Name = "Pebble texture", Value = "Pebble", IsDefault = false, SortOrder = 5 }
                        }
                    },
                    new ProductOption { Name = "Coating", InputTypeSingleOptionId = 1, InputTypeMultipleOptionId = 2, SortOrder = 4,
                        ProductOptionsDetails =
                        new List<ProductOptionsDetail>
                        {
                            new ProductOptionsDetail { Name = "None", Value = "None", IsDefault = true, SortOrder = 1 },
                            new ProductOptionsDetail { Name = "Linen texture", Value = "Linen", IsDefault = false, SortOrder = 2 },                         
                            new ProductOptionsDetail { Name = "Canvas texture", Value = "Canvas", IsDefault = false, SortOrder = 3 },
                            new ProductOptionsDetail { Name = "Watercolor texture", Value = "Canvas", IsDefault = false, SortOrder = 4 },
                            new ProductOptionsDetail { Name = "Pebble texture", Value = "Pebble", IsDefault = false, SortOrder = 5 }
                        }
                    }
                }
            },

The part I would like to return from a method would be something like: ProductOptions = getOptions() All that nested code can be repeated verbatim. I have tried working from some other examples but I keep on getting errors in Visual Studio. If I could get a very basic approach to this, that would be greatly appreciated.


public List<ProductOptionsDetail> GetOptions() {
    return new List<ProductOptionsDetail>()
        {
            new ProductOptionsDetail() { Name = "None", Value = "None", IsDefault = true, SortOrder = 1 },
            new ProductOptionsDetail() { Name = "Linen texture", Value = "Linen", IsDefault = false, SortOrder = 2 },                         
            new ProductOptionsDetail() { Name = "Canvas texture", Value = "Canvas", IsDefault = false, SortOrder = 3 },
            new ProductOptionsDetail() { Name = "Watercolor texture", Value = "Canvas", IsDefault = false, SortOrder = 4 },
            new ProductOptionsDetail() { Name = "Pebble texture", Value = "Pebble", IsDefault = false, SortOrder = 5 }
        };
}
4

1 回答 1

1
public List<ProductOptionsDetail> GetOptions() {
    return new List<ProductOptionsDetail>()
        {
            new ProductOptionsDetail() { Name = "None", Value = "None", IsDefault = true, SortOrder = 1 },
            new ProductOptionsDetail() { Name = "Linen texture", Value = "Linen", IsDefault = false, SortOrder = 2 },                         
            new ProductOptionsDetail() { Name = "Canvas texture", Value = "Canvas", IsDefault = false, SortOrder = 3 },
            new ProductOptionsDetail() { Name = "Watercolor texture", Value = "Canvas", IsDefault = false, SortOrder = 4 },
            new ProductOptionsDetail() { Name = "Pebble texture", Value = "Pebble", IsDefault = false, SortOrder = 5 }
        };
}
于 2013-05-03T02:55:11.023 回答