我正在使用EF并为我们的数据库添加一些产品数据。我播种的数据有一部分将重复约100次。而不是复制和粘贴我的代码,我宁愿用一个方法填充我的列表,但由于我是一个新手,我似乎无法正常工作:
以下是上下文中的代码:
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 }
}
}
}
},
我想从一个方法返回的部分是这样的: ProductOptions = getOptions()所有嵌套代码都可以逐字重复。我尝试过其他一些例子,但我一直在Visual Studio中遇到错误。如果我能得到一个非常基本的方法,那将非常感激。
答案 0 :(得分: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 }
};
}