我正在尝试使用 Entity Framework 更新连接表,但我没有走远,因为我收到了一个转换错误:
无法将类型转换为 System.Data.Objects.DataClasses.EntityCollection
因此,当我尝试将乘客对象传递给 Flight 实体时,它无法进行强制转换。
这是表结构:
Flight table - FlightID PK
Passenger Table - Passenger ID PK
Passenger_Flight table - PassengerID PK,FlightID PK
if (isValid())
{
Passenger cpass = ((Passenger)passengerIDComboBox.SelectedItem);
Flight newflight = new Flight {
Rego = planeRegoComboBox.SelectedItem.ToString(),
DepartureTime = departureDateTimePicker.Value,
ArrivalTime = arrivalDateTimePicker.Value,
DepartureLocation = departureLocationComboBox.GetItemText(departureLocationComboBox.SelectedItem),
ArrivalLocation = arrivalLocationComboBox.GetItemText(arrivalLocationComboBox.SelectedItem),
Passengers = cpass // this is the problem. Cannot convert type to System.Data.Objects.DataClasses.EntityCollection
};
flightEntity.Flights.AddObject(newflight);
Validate();
//flightEntity.Passengers.AddObject(pass);
flightEntity.SaveChanges();
refreshItems();
}
提前致谢。我希望我提供了足够的信息。这个问题链接到https://stackoverflow.com/questions/19087747/an-object-with-a-temporary-entitykey-value-cannot-be-attached-to-an-object-cont