我是 c# 的新手。我有一个关于实体 edmx 对象中与外键绑定的组合框的问题。
我有具有字段的“客户卡”edmx 实体对象,其中一个字段是该客户的外键 (CityID),其中包含 Cities 表或“City”实体对象列表的 ID。
我想在我的代码中管理客户 City 组合框。我正在填充组合框。还可以在用户保存记录时检索当前选定的项目。然而; 我不确定如何将组合项目中的选定项目分配回客户实体对象。我在实体对象中看到的只是 City 和 CityReference 属性。我的问题是如何将当前城市ID选择项分配给实体对象中的“客户卡”?请附上代码。
这是我的代码
private class CustCard
{
public int ID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
}
public class CityCard
{
public int ID {get; set;}
public string Name {get; set;}
public string Country {get; set;}
}
var custCards = from p in entities.CustomerCards
select new CustCard
{
ID = p.ID,
CompanyName = p.CompanyName,
ContactName = p.ContactName,
City = p.City.Name,
CityID = p.City.ID,
};
var cityList = from p in entities.Cities
select new CityCard
{
ID = p.ID,
Name = p.Name,
Country = p.Country.Name,
};
//Bind City List
CityComboBox.DataSource = CityList; //cities in the entity object
CityComboBox.ValueMember = "ID";
CityComboBox.DisplayMember = "Name";
private void Save_Click(object sender, EventArgs e)
{
CustomerCard Cust = new CustomerCard();
Cust.Address = addressTextBox.Text;
Cust.CompanyName = companyNameTextBox.Text;
**// HOW CAN I assign Selected item to the City field in the CustCard
????????????cityIDComboBox.SelectedItem;?????????????**
entities.AddToCustomerCards(Cust);
entities.SaveChanges();
}