2016-04-16 91 views
0

我很難理解如何在使用實體框架時將項目添加到列表中。 我的數據庫已經創建並且有表格,但是當我嘗試通過我的種子方法添加選項時,我的選項表格保持爲空。如何使用實體框架將項目添加到列表屬性

如何正確添加我的選項,以便它們出現在我的數據庫中並鏈接到正確的車輛?

我有2種型號:

車輛

public class VehicleModels 
    { 

     public virtual int Id { get; set; } 
     public virtual string Naam { get; set; } 
     public virtual string Merk { get; set; } 
     public virtual string Brandstof { get; set; } 
     public virtual string Kleur { get; set; } 
     public virtual string TypeVanMerk { get; set; } 
     public virtual string TypeVanTransmissie { get; set; } 
     public virtual int Kilometerstand { get; set; } 
     public virtual int Bouwjaar { get; set; } 
     public virtual int AantalDeuren { get; set; } 
     public List<Optie> Options { get; set; } 

     public VehicleModels() 
     { 
      Options = new List<Optie>(); 
     } 

     public void AddOption(Optie optie) { 
      if (!Options.Equals(null)) 
      { 
       Options.Add(optie); 
      } 
     } 


    } 

而且選項

public class Optie 
    { 
     public virtual int OptieId { get; set; } 
     public virtual string Naam { get; set; } 

     public Optie(string naam) 
     { 
      Naam = naam; 
     } 
    } 

這是我的種子/初始值設定

public class VehicleDbInitializer : DropCreateDatabaseAlways<VehicleDB> 
    { 

     protected override void Seed(VehicleDB context) 
     { 
      context.VehicleModels.Add(new VehicleModels 
      { 
       Naam = "Auto 1", 
       Merk = "Audi", 
       Brandstof = "Benzine", 
       Kleur = "Rood", 
       TypeVanMerk = "A6", 
       TypeVanTransmissie = "Manueel", 
       Kilometerstand = 60000, 
       Bouwjaar = 2002, 
       AantalDeuren = 5 
      }); 
      context.VehicleModels.Add(new VehicleModels 
      { 
       Naam = "Auto 2", 
       Merk = "BMW", 
       Brandstof = "Diesel", 
       Kleur = "Silver", 
       TypeVanMerk = "6", 
       TypeVanTransmissie = "Manueel", 
       Kilometerstand = 25000, 
       Bouwjaar = 2012, 
       AantalDeuren = 3 
      }); 

      VehicleModels vehicle = new VehicleModels(); 
      vehicle.Naam = "Auto 3"; 
      vehicle.Merk = "Volvo"; 
      vehicle.Brandstof = "Diesel"; 
      vehicle.Kleur = "Silver"; 
      vehicle.TypeVanMerk = "6"; 
      vehicle.TypeVanTransmissie = "Manueel"; 
      vehicle.Kilometerstand = 25000; 
      vehicle.Bouwjaar = 2012; 
      vehicle.AantalDeuren = 3; 


      vehicle.Options.Add(new Optie("Airco")); 
      vehicle.Options.Add(new Optie("Trekhaak")); 
      vehicle.Options.Add(new Optie("Airbags")); 

      context.VehicleModels.Add(vehicle); 

      base.Seed(context); 
     } 
    } 

Global.aspx.cs

public class MvcApplication : System.Web.HttpApplication 
    { 
     protected void Application_Start() 
     { 
      Database.SetInitializer(new VehicleDbInitializer()); 

      AreaRegistration.RegisterAllAreas(); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 
     } 
    } 
+0

分享您Global.aspx代碼嗎? –

+0

@NazmulHasan done – Vahx

+0

你確定你的種子方法調用?你對遷移有什麼想法嗎?然後請申請。發生什麼事? –

回答

0

你忘了在也許你種方法的最後調用context.SaveChanges()

+0

欣賞努力,但沒有效果在Seed方法中創建數據庫。我在這個項目中使用的任何文檔中都沒有提到這一點。 – Vahx

-1

您需要添加以下內容:

private List<Optie> tempList= new List<Optie>();  
public List<Optie> Options 
{ 
    get 
    { 
     return tempList; 
    } 
} 

並刪除以下內容:

public VehicleModels() 
{ 
    Options = new List<Optie>(); 
} 

public void AddOption(Optie optie) { 
    if (!Options.Equals(null)) 
    { 
     Options.Add(optie); 
    } 
} 
相關問題