1

I'm using code-first, and am trying to generate some sample data, but am stuck.

       var ts = new List<VehicleType>
        {       new VehicleType { TypeName = "Car" },
                new VehicleType { TypeName = "Truck" },
                new VehicleType { TypeName = "Van" },
                new VehicleType { TypeName = "SUV" },
                new VehicleType { TypeName = "RV" }
            };


        var mr = new List<Manufacturer>{
                new Manufacturer { ManufacturerName = "Ford" },
                new Manufacturer { ManufacturerName = "Dodge" },
                new Manufacturer { ManufacturerName = "Hyundai" },
                new Manufacturer { ManufacturerName = "Mazda" },
                new Manufacturer { ManufacturerName = "Honda" },
                new Manufacturer { ManufacturerName = "Lexus" },
                new Manufacturer { ManufacturerName = "Cadillac" },
                new Manufacturer { ManufacturerName = "Jayco" }
    };

        var vm = new List<VehicleModel>{
                new VehicleModel { ModelName = "Focus" },
                new VehicleModel { ModelName = "Elantra" },
                new VehicleModel { ModelName = "3" },
                new VehicleModel { ModelName = "4X4" },
                new VehicleModel { ModelName = "CRX" },
                new VehicleModel { ModelName = "Element" },
                new VehicleModel { ModelName = "SE 541" },
                new VehicleModel { ModelName = "LE 4X" }
    };

        new List<Inventory>
        {
            new Inventory {VehicleModel = vm.Single(g => g.ModelName == "Focus"), Manufacturer = mr.Single(g => g.ManufacturerName == "Ford"), VehicleType = ts.Single(g => g.TypeName == "Car"), Price = 10999, Mileage = 241092, Year = 2001, Description = "Includes all G37x AWD Sport standard equipment FORD", Colour = "Tan", CarImageUrl = "/Content/Images/car1.jpg", DateReceived = "01/06/2011"}, 

}.ForEach(a => context.Inventorys.Add(a));

This works fine. My Inventory table in populated with the data from the other tables (VehicleType, Manufacturer, and VehicleModel). Here are their models:

public class Manufacturer
{
    [Key]
    public int ManufacturerId { get; set; }
    public string ManufacturerName { get; set; }

}

public class VehicleModel
{
    [Key]
    public int ModelID { get; set; }
    public string ModelName { get; set; }
}
public class VehicleType
{
    [Key]
    public int TypeId { get; set; }
    public string TypeName { get; set; }
}

Now lets say the Manufacturer model had another attribute besides ManufacturerName, let's say it also has ManufacturerLocation. When I try to add that attribute into the Inventory list:

new List<Inventory>
        {
new Inventory {VehicleModel = vm.Single(g => g.ModelName == "Focus"), Manufacturer = mr.Single(g => g.SomeAttributeFromManufacturerTable)

All attributes from the Manufacturer table will appear, but after I use one of those attributes, there doesn't seem to be an opportunity to enter in the rest of the attributes. Is this because it expects me to enter in the ManufacturerId attribute, and it will automatically associate the rest on the attributes through its primary key? I hope I'm making sense, I find this hard to explain.

Christopher Hunt
  • 107
  • 1
  • 3
  • 10

0 Answers0