简体   繁体   中英

How do i add items to a list property using Entity Framework

I am having difficulties understanding how to add items to my list while using Entity Framework. My database has been created and the tables are there, but the table for my options stays empty when i try to add options through my seed method.

How do i add my options correctly so that they appear in my database and are linked to the correct vehicle?

I have 2 models:

Vehicle

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);
            }  
        }


    }

And Option

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

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

And this is my Seed/Initializer

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);
        }
    }

您忘记在Seed方法的末尾调用context.SaveChanges()吗?

You need to add following:

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

and remove following:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM