public VehicleWithDetail VehicleAdd(VehicleAdd newItem)
{
// This method is called from the Vehicles controller...
// ...AND the Manufacturers controller
// When adding an object with a required to-one association,
// MUST fetch the associated object first
// Attempt to find the associated object
var a = ds.Manufacturers.Find(newItem.ManufacturerId);
if (a == null)
{
return null;
}
else
{
// Attempt to add the new item
var addedItem = ds.Vehicles.Add(Mapper.Map<Vehicle>(newItem));
// Set the associated item property
addedItem.Manufacturer = a;
ds.SaveChanges();
return (addedItem == null) ? null : Mapper.Map<VehicleWithDetail>(addedItem);
}
}