2017-04-20 76 views
0

我是C#的新手。我正在開發一個web應用程序項目。我想知道如何在我的DbInitializer類中初始化列表。例如,這是模型:從模型類傳遞給DbInitliazer

using System; 
using System.Collections.Generic; 

namespace Manager.Model 
{ 

    public class Vendor 
    { 
     public int VendorID { get; set; } 
     public string CardName { get; set; } 
     public string WebsiteLink { get; set; } 
     public DateTime PartnerSince { get; set; } 
     public List<Rep> Reps { get; set; } 
     public string SupportNo { get; set; } 
     public string SupportEmail { get; set; } 
     public string Rebate { get; set; } 
     public string Spiff { get; set; } 
     public string Quote { get; set; } 
    } 
    public class Rep 
    { 
     public string RepName { get; set; } 
     public string RepPosition { get; set; } 
     public string RepNo { get; set; } 
     public string RepEmail { get; set; } 
    } 
} 

如何在Initialize方法中傳遞此列表?

public static void Initialize(ManagementContext context) 
    { 
     context.Database.EnsureCreated(); 

     // Look for any students. 
     if (context.Vendors.Any()) 
     { 
      return; // DB has been seeded 
     } 


     var vendors = new Vendor[] 
     { 
     new Vendor{CardName="Vendor1", WebsiteLink="www.vendor1.com", PartnerSince=DateTime.Parse("10-10-2012"), SupportNo="521-586-8956", SupportEmail="[email protected]"},   
     }; 

     foreach (Vendor v in vendors) 
     { 
      context.Vendors.Add(v); 
     } 
     context.SaveChanges(); 
+0

我沒有得到DBIntializer一部分,但如果你想通過列表,用戶列表 list = new List '並添加項目如'list.Add(new Vendor(){prop1 =''...});' –

+0

我想過去公開列表代表(包括代表聯繫的所有信息)到供應商對象上。我只是用整個DbInitializer代碼更新了這篇文章。 –

+0

換句話說,如何將列表項傳遞給此行:new Vendor {CardName = ...... –

回答

1

如果你想要做的一切在線:

Vendor[] vendors = new Vendor[] 
{ 
    new Vendor() // first vendor 
    { 
     CardName="Vendor1", 
     WebsiteLink="www.vendor1.com", 
     PartnerSince=DateTime.Parse("10-10-2012"), 
     SupportNo="521-586-8956", 
     SupportEmail="[email protected]", 
     Reps = new List<Rep>() 
     { 
      new Rep() // first rep 
      { 
       RepName = "name", 
       RepPosition = "pos", 
       RepNo = "no", 
       RepEmail = "email" 
      } 
      // , new Rep(){...} // second rep, etc... 
     } 
    } 
    // , new Vendor(){....} // second vendor, etc...  
}; 

或者乾脆先準備銷售代表:

List<Rep> Reps1 = new List<Rep>(); // Reps 1 for Vendor 1 
Reps1.Add(new Rep() 
{ 
    RepName = "name", 
    RepPosition = "pos", 
    RepNo = "no", 
    RepEmail = "email" 
}); 
// you may add more rep 

然後在供應商分配給它

Vendor[] vendors = new Vendor[] 
{ 
    new Vendor() // first vendor 
    { 
     CardName="Vendor1", 
     WebsiteLink="www.vendor1.com", 
     PartnerSince=DateTime.Parse("10-10-2012"), 
     SupportNo="521-586-8956", 
     SupportEmail="[email protected]", 
     Reps = Reps1 
    } 
    // , new Vendor(){....} // second vendor, etc...  
}; 

有關的問題,如果你改變成字符串[] RepNames,

string[] RepNames1 = new string[] 
{ 
    "name1", 
    "name2" // , etc.... 
} 

然後分配給它的供應商

Vendor[] vendors = new Vendor[] 
{ 
    new Vendor() // first vendor 
    { 
     CardName="Vendor1", 
     WebsiteLink="www.vendor1.com", 
     PartnerSince=DateTime.Parse("10-10-2012"), 
     SupportNo="521-586-8956", 
     SupportEmail="[email protected]", 
     RepNames = RepNames1 
    } 
    // , new Vendor(){....} // second vendor, etc...  
}; 
+0

如果我想將其更改爲數組(而我將在模型中使用公共字符串[] RepNames) ,我如何在Vendor對象中傳遞它? –

+0

最後一個問題:對於列表項目,我如何單獨顯示它們?例如,我想顯示Rep1的RepName。 –

+0

非常感謝! –