我是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();
我沒有得到DBIntializer一部分,但如果你想通過列表,用戶列表 list = new List '並添加項目如'list.Add(new Vendor(){prop1 =''...});' –
我想過去公開列表代表(包括代表聯繫的所有信息)到供應商對象上。我只是用整個DbInitializer代碼更新了這篇文章。 –
換句話說,如何將列表項傳遞給此行:new Vendor {CardName = ...... –