我使用代码优先,并试图生成一些示例数据,但被卡住了。
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));
这工作正常。我的 Inventory 表中填充了来自其他表(VehicleType、Manufacturer 和 VehicleModel)的数据。以下是他们的模型:
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; }
}
现在让我们说制造商模型除了制造商名称之外还有另一个属性,假设它还有制造商位置。当我尝试将该属性添加到库存列表中时:
new List<Inventory>
{
new Inventory {VehicleModel = vm.Single(g => g.ModelName == "Focus"), Manufacturer = mr.Single(g => g.SomeAttributeFromManufacturerTable)
制造商表中的所有属性都会出现,但在我使用其中一个属性后,似乎没有机会输入其余属性。这是因为它希望我输入 ManufacturerId 属性,它会通过其主键自动关联属性上的其余部分吗?我希望我说得通,我觉得这很难解释。