将模型类中的列表传递给DbInitliazer

时间:2017-04-20 00:05:07

标签: c# asp.net asp.net-mvc

我是C#的新手。我正在开发一个Web应用程序项目。我想知道如何在我的DbInitializer类中初始化列表。例如,这是模型:

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="nikki@vendor1.com"},          
        };

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

1 个答案:

答案 0 :(得分: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="nikki@vendor1.com",
       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...    
 };

或者只是先准备好Reps:

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="nikki@vendor1.com",
        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="nikki@vendor1.com",
        RepNames = RepNames1
     }
     // , new Vendor(){....}    // second vendor, etc...    
};