2013-05-03 23 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

相關問題