2013-05-03 128 views
0

我正在使用EF并为我们的数据库播种一些产品数据。我播种的数据有一部分会重复大约100次。而不是复制和粘贴我的代码,我宁愿填充我的名单与方法,但因为我是一个新手,我似乎无法使这项工作正常:MVC 4 C#如何创建一个返回列表的方法<object>

这是在上下文中的代码:

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中遇到错误。如果我能得到一个非常基本的方法,这将不胜感激。

回答

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 } 
     }; 
} 
+0

就是这样!谢谢。我无法相信我过于复杂的事情。再次感谢。 – 2013-05-03 03:12:03

+0

@ Ek0nomik - 使用初始化语法时,无参数构造函数不需要括号。您通常不会同时使用参数构造函数和初始化。 – 2013-05-03 03:24:41

相关问题